~josejuan-sanchez/esajpip/continue

« back to all changes in this revision

Viewing changes to src/trace.h

  • Committer: José Juan Sánchez Hernández
  • Date: 2013-10-01 10:01:21 UTC
  • Revision ID: josejuan.sanchez@gmail.com-20131001100121-xfobvkenqie7y0te
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _TRACE_H_
 
2
#define _TRACE_H_
 
3
 
 
4
 
 
5
#include <string>
 
6
#include <iostream>
 
7
 
 
8
#define LOG4CPP_FIX_ERROR_COLLISION 1
 
9
#include <log4cpp/Category.hh>
 
10
#include <log4cpp/FileAppender.hh>
 
11
#include <log4cpp/PatternLayout.hh>
 
12
#include <log4cpp/OstreamAppender.hh>
 
13
 
 
14
 
 
15
/**
 
16
 * Wrapper used by the application to handle the log/trace
 
17
 * messages by means of the log4cpp library.
 
18
 */
 
19
class TraceSystem
 
20
{
 
21
private:
 
22
  log4cpp::Category *category;
 
23
  log4cpp::Appender *appender;
 
24
  log4cpp::PatternLayout *layout;
 
25
  log4cpp::Appender *file_appender;
 
26
  log4cpp::PatternLayout *file_layout;
 
27
 
 
28
  static TraceSystem traceSystem;
 
29
 
 
30
  TraceSystem();
 
31
  virtual ~TraceSystem();
 
32
 
 
33
  bool AppendToFile_(const char *name);
 
34
 
 
35
public:
 
36
  static bool AppendToFile(const char *name)
 
37
  {
 
38
    return traceSystem.AppendToFile_(name);
 
39
  }
 
40
 
 
41
  static bool AppendToFile(const std::string& name)
 
42
  {
 
43
    return traceSystem.AppendToFile_(name.c_str());
 
44
  }
 
45
 
 
46
  static log4cpp::CategoryStream logStream()
 
47
  {
 
48
    return traceSystem.category->infoStream();
 
49
  }
 
50
 
 
51
  static log4cpp::CategoryStream errorStream()
 
52
  {
 
53
    return traceSystem.category->errorStream();
 
54
  }
 
55
 
 
56
  static log4cpp::CategoryStream traceStream()
 
57
  {
 
58
    return traceSystem.category->debugStream();
 
59
  }
 
60
};
 
61
 
 
62
 
 
63
#define _RED            "31m"
 
64
#define _GREEN          "32m"
 
65
#define _YELLOW         "33m"
 
66
#define _BLUE           "34m"
 
67
 
 
68
#ifndef NO_COLORS
 
69
#define _SET_COLOR(a)   "\033[" a
 
70
#define _RESET_COLOR()  "\033[0m"
 
71
#else
 
72
#define _SET_COLOR(a)   ""
 
73
#define _RESET_COLOR()  ""
 
74
#endif
 
75
 
 
76
#ifndef SILENT_MODE
 
77
#define LOG(a)      (TraceSystem::logStream() << a << log4cpp::eol)
 
78
#define LOGC(c, a)  (TraceSystem::logStream() << _SET_COLOR(c) << a << _RESET_COLOR() << log4cpp::eol)
 
79
#define ERROR(a)    (TraceSystem::errorStream() << _SET_COLOR(_RED) << __FILE__ << ":" << __LINE__ << ": ERROR: " << a << _RESET_COLOR() << log4cpp::eol)
 
80
#else
 
81
#define LOG(a)      {}
 
82
#define LOGC(c, a)  {}
 
83
#define ERROR(a)    {}
 
84
#endif
 
85
 
 
86
#if defined(SHOW_TRACES) && !defined(NDEBUG) && !defined(SILENT_MODE)
 
87
#define TRACE(a)    (TraceSystem::traceStream() << _SET_COLOR(_YELLOW) << __FILE__ << ":" << __LINE__ << ": TRACE: " << a << _RESET_COLOR() << log4cpp::eol)
 
88
#else
 
89
#define TRACE(a)    {}
 
90
#endif
 
91
 
 
92
#define CERR(a)     (cerr << _SET_COLOR(_RED) << a << "!" << _RESET_COLOR() << endl, -1)
 
93
 
 
94
#endif