Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Last version before merging with master
[simgrid.git] / src / xbt / xbt_log_appender_file.c
1 /* file_appender - a dumb log appender which simply prints to a file        */
2
3 /* Copyright (c) 2007-2012. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/sysdep.h"
9 #include "xbt/log_private.h"
10 #include "smpi/private.h" // to access bench_begin/end. Not ultraclean, I confess
11 #include <stdio.h>
12
13 static void append_file(xbt_log_appender_t this_, char *str) {
14   fputs(str, (FILE *) this_->data);
15 }
16
17 static void smpi_append_file(xbt_log_appender_t this_, char *str) {
18   fputs(str, (FILE *) this_->data);
19 }
20
21 static void free_(xbt_log_appender_t this_) {
22   if (this_->data != stderr)
23     fclose(this_->data);
24 }
25
26 void __smpi_bench_dont (void); // Stupid prototype
27 void __smpi_bench_dont (void) { /* I'm only a place-holder in case we link without SMPI */; }
28 void smpi_bench_begin(void) __attribute__ ((weak, alias ("__smpi_bench_dont")));
29 void smpi_bench_end(void)   __attribute__ ((weak, alias ("__smpi_bench_dont")));
30
31
32 XBT_LOG_EXTERNAL_CATEGORY(smpi); // To detect if SMPI is inited
33
34 xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
35
36   xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
37   if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
38     res->do_append = smpi_append_file;
39   else
40     res->do_append = append_file;
41   res->free_ = free_;
42   if (arg)
43     res->data = (void *) fopen(arg, "w");
44   else
45     res->data = (void *) stderr;
46   return res;
47 }