fastcgi++  3.1alpha
A C++ FastCGI/Web API
log.hpp
Go to the documentation of this file.
1 
10 /*******************************************************************************
11 * Copyright (C) 2017 Eddie Carle [eddie@isatec.ca] *
12 * *
13 * This file is part of fastcgi++. *
14 * *
15 * fastcgi++ is free software: you can redistribute it and/or modify it under *
16 * the terms of the GNU Lesser General Public License as published by the Free *
17 * Software Foundation, either version 3 of the License, or (at your option) *
18 * any later version. *
19 * *
20 * fastcgi++ is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for *
23 * more details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with fastcgi++. If not, see <http://www.gnu.org/licenses/>. *
27 *******************************************************************************/
28 
29 #ifndef FASTCGIPP_LOG_HPP
30 #define FASTCGIPP_LOG_HPP
31 
32 #include "fastcgi++/config.hpp"
33 
34 #include <ostream>
35 #include <mutex>
36 #include <cstdlib>
37 #include <string>
38 
40 namespace Fastcgipp
41 {
43  namespace Logging
44  {
46  extern std::wostream* logstream;
47 
49  extern std::mutex mutex;
50 
52  extern std::wstring hostname;
53 
55  extern std::wstring program;
56 
58  extern bool suppress;
59 
61  enum Level
62  {
63  INFO = 0,
64  FAIL = 1,
65  ERROR = 2,
66  WARNING = 3,
67  DEBUG = 4,
68  DIAG = 5,
69  };
70 
72  void header(Level level);
73  }
74 }
75 
77 #define INFO_LOG(data) {\
78  if(!::Fastcgipp::Logging::suppress)\
79  { \
80  std::lock_guard<std::mutex> lock(::Fastcgipp::Logging::mutex);\
81  ::Fastcgipp::Logging::header(::Fastcgipp::Logging::INFO);\
82  *::Fastcgipp::Logging::logstream << data << std::endl;\
83  }}
84 
86 
91 #define FAIL_LOG(data) {\
92  if(!::Fastcgipp::Logging::suppress)\
93  { \
94  std::lock_guard<std::mutex> lock(::Fastcgipp::Logging::mutex);\
95  ::Fastcgipp::Logging::header(::Fastcgipp::Logging::FAIL);\
96  *::Fastcgipp::Logging::logstream << data << std::endl;\
97  std::exit(EXIT_FAILURE);\
98  }}
99 
100 #if FASTCGIPP_LOG_LEVEL > 0
101 
107 #define ERROR_LOG(data) \
108  { \
109  std::lock_guard<std::mutex> lock(::Fastcgipp::Logging::mutex);\
110  ::Fastcgipp::Logging::header(::Fastcgipp::Logging::ERROR);\
111  *::Fastcgipp::Logging::logstream << data << std::endl;\
112  }
113 #else
114 #define ERROR_LOG(data) {}
115 #endif
116 
117 #if FASTCGIPP_LOG_LEVEL > 1
118 
124 #define WARNING_LOG(data) {\
125  if(!::Fastcgipp::Logging::suppress)\
126  { \
127  std::lock_guard<std::mutex> lock(::Fastcgipp::Logging::mutex);\
128  ::Fastcgipp::Logging::header(::Fastcgipp::Logging::WARNING);\
129  *::Fastcgipp::Logging::logstream << data << std::endl;\
130  }}
131 #else
132 #define WARNING_LOG(data) {}
133 #endif
134 
135 #if FASTCGIPP_LOG_LEVEL > 2
136 #define DEBUG_LOG(data) {\
138  if(!::Fastcgipp::Logging::suppress)\
139  { \
140  std::lock_guard<std::mutex> lock(::Fastcgipp::Logging::mutex);\
141  ::Fastcgipp::Logging::header(::Fastcgipp::Logging::DEBUG);\
142  *::Fastcgipp::Logging::logstream << data << std::endl;\
143  }}
144 #else
145 #define DEBUG_LOG(data) {}
146 #endif
147 
148 #if FASTCGIPP_LOG_LEVEL > 3
149 #define DIAG_LOG(data) {\
151  if(!::Fastcgipp::Logging::suppress)\
152  { \
153  std::lock_guard<std::mutex> lock(::Fastcgipp::Logging::mutex);\
154  ::Fastcgipp::Logging::header(::Fastcgipp::Logging::DIAG);\
155  *::Fastcgipp::Logging::logstream << data << std::endl;\
156  }}
157 #else
158 #define DIAG_LOG(data) {}
159 #endif
160 
161 #endif
Fastcgipp::Logging::FAIL
@ FAIL
Definition: log.hpp:98
config.hpp
Fastcgipp::Logging::INFO
@ INFO
Definition: log.hpp:97
Fastcgipp::Logging::program
std::wstring program
Application name to use in loggin.
Fastcgipp::Logging::hostname
std::wstring hostname
Hostname to use in logging.
Fastcgipp::Logging::mutex
std::mutex mutex
Thread safe the logging mechanism.
Definition: log.cpp:102
Fastcgipp::Logging::ERROR
@ ERROR
Definition: log.hpp:99
Fastcgipp::Logging::Level
Level
Communicate the log level to the header generator.
Definition: log.hpp:95
Fastcgipp::Logging::WARNING
@ WARNING
Definition: log.hpp:100
Fastcgipp::Logging::suppress
bool suppress
Set to true if you want to suppress non-error logs.
Fastcgipp
Topmost namespace for the fastcgi++ library.
Definition: fcgistreambuf.cpp:34
Fastcgipp::Logging::DIAG
@ DIAG
Definition: log.hpp:102
Fastcgipp::Logging::DEBUG
@ DEBUG
Definition: log.hpp:101
Fastcgipp::Logging::logstream
std::wostream * logstream
The actual stream we will be logging to.
Fastcgipp::Logging::header
void header(Level level)
Send a log header to logstream.
Definition: log.cpp:107