Leosac  0.8.0
Open Source Access Control
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::WARN:
80  if (console)
81  console->warn(log_msg_with_thread_id);
82  if (syslog)
83  syslog->warn(remove_ascii_format(log_msg_with_thread_id));
84  break;
85  case LogLevel::ERROR:
86  if (console)
87  console->error(log_msg_with_thread_id);
88  if (syslog)
89  syslog->error(remove_ascii_format(log_msg_with_thread_id));
90  break;
91  case LogLevel::CRIT:
92  if (console)
93  console->critical(log_msg_with_thread_id);
94  if (syslog)
95  syslog->critical(remove_ascii_format(log_msg_with_thread_id));
96  break;
97  }
98 }
99 
100 LogLevel log_level_from_string(const std::string &level)
101 {
102  if (level == "DEBUG")
103  return LogLevel::DEBUG;
104  else if (level == "INFO")
105  return LogLevel::INFO;
106  else if (level == "WARNING")
107  return LogLevel::WARN;
108  else if (level == "ERROR")
109  return LogLevel::ERROR;
110  else if (level == "CRIT")
111  return LogLevel::CRIT;
112  throw std::runtime_error("Invalid log level: " + level);
113 }
114 }
Leosac::gettid
uint64_t gettid()
Return the Linux thread ID.
Definition: ThreadUtils.cpp:32
WARN
@ WARN
Definition: log.hpp:33
ERROR
@ ERROR
Definition: log.hpp:32
DEBUG
@ DEBUG
Definition: log.hpp:35
LogHelper
Definition: log.cpp:44
INFO
@ INFO
Definition: log.hpp:34
ThreadUtils.hpp
CRIT
@ CRIT
Definition: log.hpp:31
log.hpp
LogHelper::log_level_from_string
LogLevel log_level_from_string(const std::string &level)
Definition: log.cpp:100
LogHelper::log
void log(const std::string &log_msg, int, const char *, const char *, LogLevel level)
Definition: log.cpp:47
LogLevel
LogLevel
Definition: log.hpp:29