File: logging.hpp

package info (click to toggle)
netgen 6.2.2601%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,076 kB
  • sloc: cpp: 166,627; tcl: 6,310; python: 2,868; sh: 528; makefile: 90
file content (117 lines) | stat: -rw-r--r-- 3,291 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef NETGEN_CORE_LOGGING_HPP
#define NETGEN_CORE_LOGGING_HPP

#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "exception.hpp"
#include "ngcore_api.hpp"
#include "utils.hpp"

#ifndef NETGEN_DEBUG_LOG
#define NETGEN_DEBUG_LOG(logger, ...)
#endif  // NETGEN_DEBUG_LOG

namespace spdlog
{
  class logger;
} // namespace spdlog

namespace ngcore
{
  NGCORE_API extern std::ostream* testout; // NOLINT
  
  namespace level
  {
    enum level_enum
    {
      trace = 0,
      debug = 1,
      info = 2,
      warn = 3,
      err = 4,
      critical = 5,
      off = 6
    };
  } // namespace level

  class Logger
  {
    static NGCORE_API level::level_enum global_level;

  public:
    static auto SetGlobalLoggingLevel( level::level_enum level )
    {
      auto oldval = global_level;
      global_level = level;
      return oldval;
    }

    std::shared_ptr<spdlog::logger> logger;

    Logger(std::shared_ptr<spdlog::logger> l) : logger(std::move(l)) {}

    void NGCORE_API log( level::level_enum level, std::string && s);

    template<typename T>
    std::string replace(std::string s, const T & t)
    {
      auto p0 = s.find_first_of('{');
      auto p1 = s.find_first_of('}', p0);
      if(p0==std::string::npos || p1==std::string::npos)
        throw Exception("invalid format string");
      s.replace(p0, p1-p0+1, ToString(t));
      return s;
    }

    std::string log_helper(std::string s)
    {
      return s;
    }

    template<typename T>
    std::string log_helper(std::string s,  const T &t)
    {
      return replace(s,t);
    }

    template<typename T, typename ... Args>
    std::string log_helper( std::string s, const T &t, Args ... args)
    {
      return log_helper(replace(s,t), args...);
    }

    template<typename ... Args>
    void log( level::level_enum level, const char* str, Args ... args)
    {
      log(level, log_helper(std::string(str), args...));
    }

    template<typename ... Args>
    void trace( const char* str, Args ... args) { log(level::level_enum::trace, str, args...); }
    template<typename ... Args>
    void debug( const char* str, Args ... args) { log(level::level_enum::debug, str, args...); }
    template<typename ... Args>
    void info( const char* str, Args ... args) { log(level::level_enum::info, str, args...); }
    template<typename ... Args>
    void warn( const char* str, Args ... args) { log(level::level_enum::warn, str, args...); }
    template<typename ... Args>
    void error( const char* str, Args ... args) { log(level::level_enum::err, str, args...); }
    template<typename ... Args>
    void critical( const char* str, Args ... args) { log(level::level_enum::critical, str, args...); }
  };




  NGCORE_API std::shared_ptr<Logger> GetLogger(const std::string& name);
  NGCORE_API void SetLoggingLevel(level::level_enum level, const std::string& name);
  NGCORE_API void AddFileSink(const std::string& filename, level::level_enum level, const std::string& logger);
  NGCORE_API void AddConsoleSink(level::level_enum level, const std::string& logger);
  NGCORE_API void ClearLoggingSinks(const std::string& logger);
  NGCORE_API void FlushOnLoggingLevel(level::level_enum level, const std::string& logger);
} // namespace ngcore

#endif // NETGEN_CORE_LOGGING_HPP