File: logger.h

package info (click to toggle)
aoflagger 3.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,000 kB
  • sloc: cpp: 67,891; python: 497; sh: 242; makefile: 22
file content (102 lines) | stat: -rw-r--r-- 2,520 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef AOFLAGGER
#define AOFLAGGER

#include <sstream>
#include <iostream>

#include <boost/thread/mutex.hpp>

class Logger {
 public:
  enum LoggerLevel {
    NoLevel = 5,
    FatalLevel = 4,
    ErrorLevel = 3,
    WarningLevel = 2,
    InfoLevel = 1,
    DebugLevel = 0
  };

  enum VerbosityLevel { QuietVerbosity, NormalVerbosity, VerboseVerbosity };

  template <enum LoggerLevel Level, bool ToStdErr = false>
  class LogWriter {
   public:
    LogWriter() : _atNewLine(true) {}

    LogWriter& operator<<(const std::string& str) {
      boost::mutex::scoped_lock lock(_mutex);
      size_t start = 0, end;
      while (std::string::npos != (end = str.find('\n', start))) {
        outputLinePart(str.substr(start, end - start + 1), true);
        start = end + 1;
      }
      outputLinePart(str.substr(start, str.size() - start), false);
      return *this;
    }
    LogWriter& operator<<(const char* str) {
      (*this) << std::string(str);
      return *this;
    }
    LogWriter& operator<<(const char c) {
      boost::mutex::scoped_lock lock(_mutex);
      outputLinePart(std::string(1, c), c == '\n');
      return *this;
    }
    template <typename S>
    LogWriter& operator<<(const S& str) {
      std::ostringstream stream;
      stream << str;
      (*this) << stream.str();
      return *this;
    }
    void Flush() {
      boost::mutex::scoped_lock lock(_mutex);
      if (ToStdErr)
        std::cerr.flush();
      else
        std::cout.flush();
    }

   private:
    boost::mutex _mutex;
    bool _atNewLine;

    void outputLinePart(const std::string& str, bool endsWithCR) {
      if ((int)_coutLevel <= (int)Level && !str.empty()) {
        if (_atNewLine && _logTime) outputTime(ToStdErr);
        if (ToStdErr)
          std::cerr << str;
        else
          std::cout << str;
        _atNewLine = endsWithCR;
      }
    }
  };

  static void SetVerbosity(VerbosityLevel verbosityLevel);

  static bool IsVerbose() { return _coutLevel == DebugLevel; }

  static void SetLogTime(bool logTime) { _logTime = logTime; }

  static bool LogTime() { return _logTime; }

  static class LogWriter<DebugLevel> Debug;
  static class LogWriter<InfoLevel> Info;
  static class LogWriter<WarningLevel> Warn;
  static class LogWriter<ErrorLevel, true> Error;
  static class LogWriter<FatalLevel, true> Fatal;
  static class LogWriter<NoLevel> Progress;

 private:
  Logger() {}

  static void outputTime(bool toStdErr);

  static enum LoggerLevel _coutLevel;

  static bool _logTime;
};

#endif