STXXL  1.4.1
 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 #ifndef STXXL_THREAD_ID
22 # if STXXL_STD_THREADS || STXXL_BOOST_THREADS
23 # define STXXL_THREAD_ID (-1)
24 # else
25 # define STXXL_THREAD_ID pthread_self()
26 # endif
27 #endif
28 
30 
31 static const double program_start_time_stamp = timestamp();
32 
33 void print_msg(const char* label, const std::string& msg, unsigned flags)
34 {
35  std::string s;
36 #ifdef STXXL_PRINT_TIMESTAMP_ALWAYS
37  const bool timestamp_always = true;
38 #else
39  const bool timestamp_always = false;
40 #endif
41  if (timestamp_always || (flags & _STXXL_PRNT_TIMESTAMP)) {
42  double t = timestamp() - program_start_time_stamp;
43  char tstr[23]; /* "[364:23:59:59.999999] " */
44  snprintf(tstr, sizeof(tstr), "[%d.%02d:%02d:%02d.%06d] ",
45  int(t / (24 * 60 * 60)),
46  int(t / (60 * 60)) % 24,
47  int(t / 60) % 60, int(t) % 60,
48  int((t - floor(t)) * 1000000));
49  s += tstr;
50  }
51  if (label) {
52  s += '[';
53  s += label;
54  s += "] ";
55  }
56  if (flags & _STXXL_PRNT_THREAD_ID) {
57  char tstr[32];
58  snprintf(tstr, sizeof(tstr), "[T%ld] ", long(STXXL_THREAD_ID));
59  s += tstr;
60  }
61  s += msg;
62  if (flags & _STXXL_PRNT_ADDNEWLINE)
63  s += '\n';
64  if (flags & _STXXL_PRNT_COUT)
65  std::cout << s << std::flush;
66  if (flags & _STXXL_PRNT_CERR)
67  std::cerr << s << std::flush;
68  logger* logger_instance = logger::get_instance();
69  if (flags & _STXXL_PRNT_LOG)
70  logger_instance->log_stream() << s << std::flush;
71  if (flags & _STXXL_PRNT_ERRLOG)
72  logger_instance->errlog_stream() << s << std::flush;
73 }
74 
76 
77 // vim: et:ts=4:sw=4
#define STXXL_THREAD_ID
Definition: verbose.cpp:25
#define _STXXL_PRNT_CERR
Definition: verbose.h:25
#define _STXXL_PRNT_COUT
Definition: verbose.h:24
#define _STXXL_PRNT_ADDNEWLINE
Definition: verbose.h:28
std::ofstream & log_stream()
Definition: log.h:36
#define _STXXL_PRNT_TIMESTAMP
Definition: verbose.h:29
#define _STXXL_PRNT_THREAD_ID
Definition: verbose.h:30
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define _STXXL_PRNT_LOG
Definition: verbose.h:26
void print_msg(const char *label, const std::string &msg, unsigned flags)
Definition: verbose.cpp:33
double timestamp()
Returns number of seconds since the epoch, high resolution.
Definition: timer.h:44
std::ofstream & errlog_stream()
Definition: log.h:41
#define _STXXL_PRNT_ERRLOG
Definition: verbose.h:27
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
static const double program_start_time_stamp
Definition: verbose.cpp:31