FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
log.h
1 #ifndef __MY_LOG_H__
2 #define __MY_LOG_H__
3 
4 /*
5  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of libcommon.
9  * This library is used by SAFS and FlashGraph, and potentially by other
10  * systems.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24 
25 #include <iostream>
26 
27 enum c_log_level
28 {
29  debug,
30  info,
31  warning,
32  error,
33  fatal,
34 };
35 
36 #ifdef USE_BOOST_LOG
37 
38 #include <boost/log/trivial.hpp>
39 
40 static inline void set_log_level(enum c_log_level level)
41 {
42  boost::log::core::get()->set_filter(
43  boost::log::trivial::severity > boost::log::trivial::info);
44 }
45 
46 #else
47 
48 class simple_log_stream
49 {
50  static c_log_level curr_log_level;
51  c_log_level level;
52  bool log_data;
53 public:
54  simple_log_stream(c_log_level level) {
55  this->level = level;
56  log_data = false;
57  }
58 
59  ~simple_log_stream() {
60  if (!log_data && level >= curr_log_level)
61  std::cout << "\n" << std::flush;
62  }
63 
64  static void set_global_log_level(c_log_level level) {
65  curr_log_level = level;
66  }
67 
68  template<class T>
69  simple_log_stream operator<<(const T &v) {
70  log_data = true;
71  if (level >= curr_log_level)
72  std::cout << v;
73  return simple_log_stream(level);
74  }
75 };
76 
77 extern simple_log_stream log_debug;
78 extern simple_log_stream log_info;
79 extern simple_log_stream log_warning;
80 extern simple_log_stream log_error;
81 extern simple_log_stream log_fatal;
82 
83 #define BOOST_LOG_TRIVIAL(x) \
84  log_##x
85 
86 static inline void set_log_level(enum c_log_level level)
87 {
88  simple_log_stream::set_global_log_level(level);
89 }
90 
91 #endif
92 
93 #endif