1 /*-
2 * Copyright (c) 2012-2017 Ilya Kaliman
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <stdarg.h>
28 #include <stdio.h>
29
30 #ifdef EFP_USE_MPI
31 #include <mpi.h>
32 #endif
33
34 #include "log.h"
35
36 static void
efp_default_log_cb(const char * msg)37 efp_default_log_cb(const char *msg)
38 {
39 fprintf(stderr, "LIBEFP: %s\n", msg);
40 }
41
42 static efp_log_cb _log_cb = efp_default_log_cb;
43
44 void
efp_log(const char * fmt,...)45 efp_log(const char *fmt, ...)
46 {
47 va_list ap;
48 char msg[512];
49
50 if (_log_cb == NULL)
51 return;
52
53 #ifdef _OPENMP
54 #pragma omp master
55 #endif
56 {
57 #ifdef EFP_USE_MPI
58 int rank;
59
60 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
61
62 if (rank == 0) {
63 va_start(ap, fmt);
64 vsnprintf(msg, sizeof(msg), fmt, ap);
65 _log_cb(msg);
66 va_end(ap);
67 }
68 #else
69 va_start(ap, fmt);
70 vsnprintf(msg, sizeof(msg), fmt, ap);
71 _log_cb(msg);
72 va_end(ap);
73 #endif
74 }
75 }
76
77 void
efp_set_log_cb(efp_log_cb log_cb)78 efp_set_log_cb(efp_log_cb log_cb)
79 {
80 _log_cb = log_cb;
81 }
82
83 efp_log_cb
efp_get_log_cb(void)84 efp_get_log_cb(void)
85 {
86 return _log_cb;
87 }
88