STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
verbose.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/common/verbose.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2009, 2010 Andreas Beckmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #include <iostream>
14 #include <cstdio>
15 #include <cmath>
16 #include <stxxl/bits/verbose.h>
17 #include <stxxl/bits/common/log.h>
20 
21 
22 #ifndef STXXL_THREAD_ID
23 # if STXXL_STD_THREADS || STXXL_BOOST_THREADS
24 # define STXXL_THREAD_ID (-1)
25 # else
26 # define STXXL_THREAD_ID pthread_self()
27 # endif
28 #endif
29 
30 
32 
33 static const double program_start_time_stamp = timestamp();
34 
35 void print_msg(const char* label, const std::string& msg, unsigned flags)
36 {
37  std::string s;
38 #ifdef STXXL_PRINT_TIMESTAMP_ALWAYS
39  const bool timestamp_always = true;
40 #else
41  const bool timestamp_always = false;
42 #endif
43  if (timestamp_always || (flags & _STXXL_PRNT_TIMESTAMP)) {
44  double t = timestamp() - program_start_time_stamp;
45  char tstr[23]; /* "[364:23:59:59.999999] " */
46  snprintf(tstr, sizeof(tstr), "[%d.%02d:%02d:%02d.%06d] ",
47  int(t / (24 * 60 * 60)),
48  int(t / (60 * 60)) % 24,
49  int(t / 60) % 60, int(t) % 60,
50  int((t - floor(t)) * 1000000));
51  s += tstr;
52  }
53  if (label) {
54  s += '[';
55  s += label;
56  s += "] ";
57  }
58  if (flags & _STXXL_PRNT_THREAD_ID) {
59  char tstr[32];
60  snprintf(tstr, sizeof(tstr), "[T%ld] ", long(STXXL_THREAD_ID));
61  s += tstr;
62  }
63  s += msg;
64  if (flags & _STXXL_PRNT_ADDNEWLINE)
65  s += '\n';
66  if (flags & _STXXL_PRNT_COUT)
67  std::cout << s << std::flush;
68  if (flags & _STXXL_PRNT_CERR)
69  std::cerr << s << std::flush;
70  logger* logger_instance = logger::get_instance();
71  if (flags & _STXXL_PRNT_LOG)
72  logger_instance->log_stream() << s << std::flush;
73  if (flags & _STXXL_PRNT_ERRLOG)
74  logger_instance->errlog_stream() << s << std::flush;
75 }
76 
78 
79 // vim: et:ts=4:sw=4
#define STXXL_THREAD_ID
Definition: verbose.cpp:26
#define _STXXL_PRNT_CERR
Definition: verbose.h:26
#define _STXXL_PRNT_COUT
Definition: verbose.h:25
#define _STXXL_PRNT_ADDNEWLINE
Definition: verbose.h:29
std::ofstream & log_stream()
Definition: log.h:37
#define _STXXL_PRNT_TIMESTAMP
Definition: verbose.h:30
#define _STXXL_PRNT_THREAD_ID
Definition: verbose.h:31
double timestamp()
Returns number of seconds since the epoch, high resolution.
Definition: timer.h:42
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define _STXXL_PRNT_LOG
Definition: verbose.h:27
void print_msg(const char *label, const std::string &msg, unsigned flags)
Definition: verbose.cpp:35
std::ofstream & errlog_stream()
Definition: log.h:42
#define _STXXL_PRNT_ERRLOG
Definition: verbose.h:28
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
static const double program_start_time_stamp
Definition: verbose.cpp:33