staticlib_pion  2.0.2
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
logger.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // ---------------------------------------------------------------------
18 // pion: a Boost C++ framework for building lightweight HTTP interfaces
19 // ---------------------------------------------------------------------
20 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
21 //
22 // Distributed under the Boost Software License, Version 1.0.
23 // See http://www.boost.org/LICENSE_1_0.txt
24 //
25 
26 #ifndef STATICLIB_PION_LOGGER_HPP
27 #define STATICLIB_PION_LOGGER_HPP
28 
29 #include "staticlib/config.hpp"
30 
31 #if defined(STATICLIB_PION_DISABLE_LOGGING)
32 
33 #define STATICLIB_PION_LOG_DEBUG(LOG, MSG) { if (LOG) {} }
34 #define STATICLIB_PION_LOG_INFO(LOG, MSG) { if (LOG) {} }
35 #define STATICLIB_PION_LOG_WARN(LOG, MSG) { if (LOG) {} }
36 #define STATICLIB_PION_LOG_ERROR(LOG, MSG) { if (LOG) {} }
37 #define STATICLIB_PION_LOG_FATAL(LOG, MSG) { if (LOG) {} }
38 
39 #elif defined(STATICLIB_PION_USE_WILTON_LOGGING)
40 
41 #include <sstream>
42 #include <string>
43 
44 #include "wilton/wilton_logging.h"
45 
46 namespace staticlib {
47 namespace pion {
48 namespace logger {
49 
58 inline void log(const std::string& name, const std::string& level, const std::string& message){
59  wilton_logger_log(level.c_str(), static_cast<int>(level.length()),
60  name.c_str(), static_cast<int>(name.length()),
61  message.c_str(), static_cast<int>(message.length()));
62 }
63 
71 inline bool is_priority_enabled(const std::string& name, const std::string& level){
72  int res = 0;
73  wilton_logger_is_level_enabled(name.c_str(), static_cast<int>(name.length()),
74  level.c_str(), static_cast<int>(level.length()), std::addressof(res));
75  return res != 0;
76 }
77 
78 } // namespace
79 }
80 }
81 
82 // Logging api, used in pion, send MSG as [str1 << str2], ostringstream used to handle it.
83 #define STATICLIB_PION_LOG_TO_WILTON(LOG, LEVEL, MSG) \
84  if (logger::is_priority_enabled(LOG, LEVEL)) { \
85  std::ostringstream ostream; \
86  ostream << MSG; \
87  logger::log(LOG, LEVEL, ostream.str()); \
88  }
89 
90 #define STATICLIB_PION_LOG_DEBUG(LOG, MSG) STATICLIB_PION_LOG_TO_WILTON(LOG, "DEBUG", MSG)
91 #define STATICLIB_PION_LOG_INFO(LOG, MSG) STATICLIB_PION_LOG_TO_WILTON(LOG, "INFO", MSG)
92 #define STATICLIB_PION_LOG_WARN(LOG, MSG) STATICLIB_PION_LOG_TO_WILTON(LOG, "WARN", MSG)
93 #define STATICLIB_PION_LOG_ERROR(LOG, MSG) STATICLIB_PION_LOG_TO_WILTON(LOG, "ERROR", MSG)
94 #define STATICLIB_PION_LOG_FATAL(LOG, MSG) STATICLIB_PION_LOG_TO_WILTON(LOG, "FATAL", MSG)
95 
96 #else
97 
98 // Logging uses std::cout and std::cerr
99 #include <ctime>
100 #include <iostream>
101 #include <thread>
102 #include <string>
103 
104 #define STATICLIB_PION_LOG_TO_OSTREAM(LOG, LEVEL, MSG) \
105 { std::cout << std::time(nullptr) << ' ' << std::this_thread::get_id() << ' ' << LEVEL << ' ' << LOG << ' ' << MSG << std::endl; }
106 
107 #define STATICLIB_PION_LOG_DEBUG(LOG, MSG) STATICLIB_PION_LOG_TO_OSTREAM(LOG, "DEBUG", MSG)
108 #define STATICLIB_PION_LOG_INFO(LOG, MSG) STATICLIB_PION_LOG_TO_OSTREAM(LOG, "INFO", MSG)
109 #define STATICLIB_PION_LOG_WARN(LOG, MSG) STATICLIB_PION_LOG_TO_OSTREAM(LOG, "WARN", MSG)
110 #define STATICLIB_PION_LOG_ERROR(LOG, MSG) STATICLIB_PION_LOG_TO_OSTREAM(LOG, "ERROR", MSG)
111 #define STATICLIB_PION_LOG_FATAL(LOG, MSG) STATICLIB_PION_LOG_TO_OSTREAM(LOG, "FATAL", MSG)
112 
113 #endif
114 
115 #endif // STATICLIB_PION_LOGGER_HPP