dxlog.h
1 // Copyright (C) 2013-2016 DNAnexus, Inc.
2 //
3 // This file is part of dx-toolkit (DNAnexus platform client libraries).
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License"); you may
6 // not use this file except in compliance with the License. You may obtain a
7 // copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 // License for the specific language governing permissions and limitations
15 // under the License.
16 
17 #ifndef DXCPP_LOG_H
18 #define DXCPP_LOG_H
19 
20 #include <sstream>
21 #include <boost/thread.hpp>
22 
23 #define DXLOG(level) \
24 if (level < dx::Log::ReportingLevel()) ; \
25 else dx::Log().Get(level)
26 
27 namespace dx {
28  /*
29  * Trivial support for logging to stderr in a thread-safe manner (logging
30  * is atomic at the level of a chain of insertion (<<) operations).
31  *
32  * Profoundly inspired from: http://www.drdobbs.com/cpp/201804215.
33  */
34  enum LogLevel {
35  logDEBUG4,
36  logDEBUG3,
37  logDEBUG2,
38  logDEBUG1,
39  logDEBUG,
40  logINFO, // Default priority for a log message
41  logWARNING,
42  logERROR, // Highest priority log message
43  DISABLE_LOGGING = 15,// If ReportingLevel is set to this, nothing is logged
44  logUSERINFO = 16 // Highest level: for logs that are always shown to the user.
45  };
46 
47  class Log {
48  public:
49  Log() { };
50  ~Log();
51  std::ostringstream& Get(LogLevel l=logINFO);
52  public:
53  static LogLevel& ReportingLevel();
54  static std::string ToString(LogLevel level);
55  static void Init();
56  private:
57  //disallow copy constructor & operator =()
58  Log(const Log&);
59  Log& operator=(const Log&);
60  protected:
61  static boost::mutex mtx;
62  std::ostringstream oss;
63  };
64 }
65 
66 #endif
An executable object that can be published for others to discover.
Definition: api.cc:7
Definition: dxlog.h:47