Blame view

src/log.cpp 4.41 KB
25b0d2f8   Peter M. Groen   Setting up Loggin...
1
  #include "log.h"
84738f56   Peter M. Groen   Setting up the lo...
2
  #include "threadcontext.h"
25b0d2f8   Peter M. Groen   Setting up Loggin...
3
  
84738f56   Peter M. Groen   Setting up the lo...
4
5
6
  // std
  #include <ios>
  #include <iomanip>
106664ec   Peter M. Groen   Setting up the lo...
7
  #include <lockguard.h>
84738f56   Peter M. Groen   Setting up the lo...
8
9
10
11
12
  #include <sstream>
  #include <syslog.h>
  #include <sys/types.h>
  #include <thread>
  #include <unistd.h>
25b0d2f8   Peter M. Groen   Setting up Loggin...
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
  
  using namespace osdev::components::mqtt;
  
  namespace {
  
  std::string toString( LogLevel level )
  {
      switch (level)
      {
          case LogLevel::Trace:
              return "T";
          case LogLevel::Debug:
              return "D";
          case LogLevel::Info:
              return "I";
          case LogLevel::Warning:
              return "W";
          case LogLevel::Error:
              return "E";
      }
      return "U";
  }
  
  } // namespace
  
  const std::string Log::s_startMarker = std::string("|________________________________________________________________________________________________________________________________|");
  std::string Log::s_context = std::string();
  std::string Log::s_fileName = std::string();
  LogLevel Log::s_logLevel = LogLevel::Info;
  
  void Log::init( const std::string &context, const std::string &logFile, LogLevel logDepth )
  {
      s_logLevel = logDepth;
      s_context = context;
  
      if( !logFile.empty())
      {
          s_fileName = logFile;
      }
  }
  
  void Log::terminate()
  {
      s_context.clear();
      s_fileName.clear();
      s_logLevel = LogLevel::Info;
  }
  
  void Log::log( const std::string &category, const std::string &message, LogLevel level )
  {
106664ec   Peter M. Groen   Setting up the lo...
63
64
      std::lock_guard<std::mutex> lock(m_mutex);
  
25b0d2f8   Peter M. Groen   Setting up Loggin...
65
66
67
68
69
70
71
72
73
74
      std::string logCategory = s_context + '|' + toString( level ) + '|' + ThreadContext::instance().context() + '|' + category;
      std::string logMessage = message;
      if( logMessage.empty() )
      {
          static const std::string emptyMessage( "--" );
          logMessage = emptyMessage;
      }
      else
      {
          // Replace % signs
84738f56   Peter M. Groen   Setting up the lo...
75
          ReplaceAll( logMessage, "%", "%%" );
25b0d2f8   Peter M. Groen   Setting up Loggin...
76
77
78
79
      }
  
      writeLog( logCategory, logMessage, level );
  }
84738f56   Peter M. Groen   Setting up the lo...
80
  
106664ec   Peter M. Groen   Setting up the lo...
81
82
83
84
85
86
87
  std::string Log::fileinfoMessage( const char *file, int line, const std::string &message )
  {
      static const std::string templ("%1:%2| %3");
      QFileInfo fi(file);
      return templ.arg( fi.fileName() ).arg(line).arg(message);
  }
  
84738f56   Peter M. Groen   Setting up the lo...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  void Log::writeLog(const std::string &category, const std::string &message, LogLevel level)
  {
      if( s_fileName.empty() )
      {
          (void)level;
          // To be implemented.
          return;
      }
      else
      {
          setlogmask( LOG_UPTO( LOG_NOTICE ) );
          openlog( category.c_str(), LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1 );
  
          syslog( LOG_NOTICE, "%s", message.c_str() );
  
          closelog();
      }
  }
  
106664ec   Peter M. Groen   Setting up the lo...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  void Log::trace(const std::string &category, const std::string &message)
  {
      log(category, message, LogLevel::Trace);
  }
  
  void Log::debug(const std::string& category, const std::string& message)
  {
      log( category, message, LogLevel::Debug );
  }
  
  void Log::info(const std::string& category, const std::string& message)
  {
      log( category, message, LogLevel::Info );
  }
  
  void Log::warning(const std::string& category, const std::string& message)
  {
      log(category, message, LogLevel::Warning );
  }
  
  void Log::error(const std::string& category, const std::string& message)
  {
      log(category, message, LogLevel::Error );
  }
  
  void Log::trace(const char *file, int line, const std::string &category, const std::string &message)
  {
      log(category, fileinfoMessage(file, line, message), LogLevel::Trace );
  }
  
  void Log::debug(const char* file, int line, const std::string& category, const std::string& message)
  {
      log( category, fileinfoMessage(file, line, message), LogLevel::Debug );
  }
  
  void Log::info(const char* file, int line, const std::string& category, const std::string& message)
  {
      log( category, fileinfoMessage(file, line, message), LogLevel::Info );
  }
  
  void Log::warning(const char* file, int line, const std::string& category, const std::string& message)
  {
      log( category, fileinfoMessage(file, line, message), LogLevel::Warning);
  }
  
  void Log::error(const char* file, int line, const std::string& category, const std::string& message)
  {
      log( category, fileinfoMessage(file, line, message), LogLevel::Error );
  }
  
84738f56   Peter M. Groen   Setting up the lo...
157
158
159
160
161
162
163
164
165
166
167
  void Log::ReplaceAll( std::string &strToReplace,
                        const std::string& from_chars,
                        const std::string& to_chars )
  {
      size_t start_pos = 0;
      while( ( start_pos = strToReplace.find( from_chars, start_pos ) ) != std::string::npos )
      {
          strToReplace.replace( start_pos, from_chars.length(), to_chars );
          start_pos += to_chars.length(); // Handles case where 'to' is a substring of 'from'
      }
  }