Leosac  0.7.0
OpenSourceAccessControl
log.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "log.hpp"
21 #include "ThreadUtils.hpp"
22 #include <boost/regex.hpp>
23 
24 namespace
25 {
32 std::string remove_ascii_format(const std::string &in)
33 {
34  boost::regex regex;
35  regex.assign("\033\\[.+m(.*)\033\\[0m");
36  std::ostringstream t(std::ios::out | std::ios::binary);
37  std::ostream_iterator<char, char> oi(t);
38  boost::regex_replace(oi, in.begin(), in.end(), regex, "\\1",
39  boost::match_default | boost::format_sed);
40  return t.str();
41 }
42 }
43 
44 namespace LogHelper
45 {
46 
47 void log(const std::string &log_msg, int /*line*/, const char * /*funcName*/,
48  const char * /*fileName*/, LogLevel level)
49 {
50  auto console = spdlog::get("console");
51  auto syslog = spdlog::get("syslog");
52  if (!console && !syslog)
53  {
54  std::cerr << "Logger not set-up yet ! Will display log message as is."
55  << std::endl;
56  std::cerr << log_msg << std::endl;
57  return;
58  }
59 
60  // fixme Maybe update spdlog and use its formatting utilities.
61  std::stringstream ss;
62  ss << "[" << Leosac::gettid() << "] " << log_msg;
63  std::string log_msg_with_thread_id = ss.str();
64 
65  switch (level)
66  {
67  case LogLevel::DEBUG:
68  if (console)
69  console->debug(log_msg_with_thread_id);
70  if (syslog)
71  syslog->debug(remove_ascii_format(log_msg_with_thread_id));
72  break;
73  case LogLevel::INFO:
74  if (console)
75  console->info(log_msg_with_thread_id);
76  if (syslog)
77  syslog->info(remove_ascii_format(log_msg_with_thread_id));
78  break;
79  case LogLevel::NOTICE:
80  if (console)
81  console->notice(log_msg_with_thread_id);
82  if (syslog)
83  syslog->notice(remove_ascii_format(log_msg_with_thread_id));
84  break;
85  case LogLevel::WARN:
86  if (console)
87  console->warn(log_msg_with_thread_id);
88  if (syslog)
89  syslog->warn(remove_ascii_format(log_msg_with_thread_id));
90  break;
91  case LogLevel::ERROR:
92  if (console)
93  console->error(log_msg_with_thread_id);
94  if (syslog)
95  syslog->error(remove_ascii_format(log_msg_with_thread_id));
96  break;
97  case LogLevel::CRIT:
98  if (console)
99  console->critical(log_msg_with_thread_id);
100  if (syslog)
101  syslog->critical(remove_ascii_format(log_msg_with_thread_id));
102  break;
103  case LogLevel::ALERT:
104  if (console)
105  console->alert(log_msg_with_thread_id);
106  if (syslog)
107  syslog->alert(remove_ascii_format(log_msg_with_thread_id));
108  break;
109  case LogLevel::EMERG:
110  if (console)
111  console->emerg(log_msg_with_thread_id);
112  if (syslog)
113  syslog->emerg(remove_ascii_format(log_msg_with_thread_id));
114  break;
115  }
116 }
117 
118 LogLevel log_level_from_string(const std::string &level)
119 {
120  if (level == "DEBUG")
121  return LogLevel::DEBUG;
122  else if (level == "INFO")
123  return LogLevel::INFO;
124  else if (level == "NOTICE")
125  return LogLevel::NOTICE;
126  else if (level == "WARNING")
127  return LogLevel::WARN;
128  else if (level == "ERROR")
129  return LogLevel::ERROR;
130  else if (level == "ALERT")
131  return LogLevel::ALERT;
132  else if (level == "EMERGENCY")
133  return LogLevel::EMERG;
134  throw std::runtime_error("Invalid log level: " + level);
135 }
136 }
uint64_t gettid()
Return the Linux thread ID.
Definition: ThreadUtils.cpp:32
Definition: log.hpp:32
Definition: log.hpp:36
Definition: log.hpp:37
Definition: log.hpp:33
Definition: log.hpp:35
Definition: log.hpp:31
LogLevel
Definition: log.hpp:29
Definition: log.hpp:34
Definition: log.hpp:38
void log(const std::string &log_msg, int, const char *, const char *, LogLevel level)
Definition: log.cpp:47
LogLevel log_level_from_string(const std::string &level)
Definition: log.cpp:118