Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Various hacks to unbench the logging to disk when run in SMPI
[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   smpi_bench_end();
19   fputs(str, (FILE *) this_->data);
20   smpi_bench_begin();
21 }
22
23 static void free_(xbt_log_appender_t this_) {
24   if (this_->data != stderr)
25     fclose(this_->data);
26 }
27
28 void __smpi_bench_dont (void); // Stupid prototype
29 void __smpi_bench_dont (void) { /* I'm only a place-holder in case we link without SMPI */; }
30 void smpi_bench_begin(void) __attribute__ ((weak, alias ("__smpi_bench_dont")));
31 void smpi_bench_end(void)   __attribute__ ((weak, alias ("__smpi_bench_dont")));
32
33
34 XBT_LOG_EXTERNAL_CATEGORY(smpi); // To detect if SMPI is inited
35
36 xbt_log_appender_t xbt_log_appender_file_new(char *arg) {
37
38   xbt_log_appender_t res = xbt_new0(s_xbt_log_appender_t, 1);
39   if (_XBT_LOGV(smpi).initialized) // HACK to detect if we run in SMPI mode. Relies on MAIN__ source disposition
40     res->do_append = smpi_append_file;
41   else
42     res->do_append = append_file;
43   res->free_ = free_;
44   if (arg)
45     res->data = (void *) fopen(arg, "w");
46   else
47     res->data = (void *) stderr;
48   return res;
49 }