File: Logger.hh

package info (click to toggle)
exactimage 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,048 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (224 lines) | stat: -rw-r--r-- 6,405 bytes parent folder | download | duplicates (6)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: utility/include/Logger.hh
 * General Sound Manipulation Program is Copyright (C) 2000 - 2004
 *   Valentin Ziegler and René Rebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */
#ifndef UTILITY__LOGGER_HH__
#define UTILITY__LOGGER_HH__

#include "SplitStreamBuffer.hh"
#include <fstream>
#include "TypeInformation.hh"

namespace Utility
{
  class LogDeviceConfig
  {
  public:
    static const bool disabled = false;                   // no logging on this device at all
    static const bool thread_save = false;                // not supported yet...
    static const bool do_timestamping = true;             // print nummeric timestamps after writing to a different logdevice
    static const bool print_log_prelude = true;           // print time and date when creating/switching logfile
    //TODO:
    static const bool output_logfile_acces_errors = true; // print message to stderr on write errors
    static const bool redirect_to_stdout_on_error = true; // redirect all content to stdout after printing error message
  };


  class LogDestinationConfig
  {
  public:
    static const bool echo_log_stdout = false;            // log output will be branched to stdout
    static const bool log_to_file = true;                 // log output will be branched into file
    static const bool function_names_in_log = false;      // log messages will be prefixed with pretty function name
    static const bool context_in_log = false;             // log messages will be prefixed with context string

    static const bool echo_warn_stderr = true;            // warn options in analogy to log options
    static const bool warn_to_file = true;
    static const bool function_names_in_warn = true;
    static const bool context_in_warn = true;
  };


  // WL_TRAITS predefined configs
  class WL_Quiet
  {
  public:
    static const bool warn = false;
    static const bool log = false;
  };

  class WL_Warn
  {
  public:
    static const bool warn = true;
    static const bool log = false;
  };

  class WL_Verbose
  {
  public:
    static const bool warn = true;
    static const bool log = true;
  };



  class BasicLogDevice
  {
  protected:
    typedef uint64_t timestamp_t;
    typedef enum {std,file,both} direction;

    static timestamp_t current;
    static direction last_direction;
    static BasicLogDevice* last_device;
    

    BasicLogDevice ();

    bool TimeStamp (direction d);
    void PrintPrelude ();
    void PrintTimeStamp (std::ostream* str);

    void AllocateSplitStreams ();
    void DeallocateSplitStreams ();

    SplitStreamBuffer* split_buffer_cout;
    SplitStreamBuffer* split_buffer_cerr;
    
    std::ostream* split_stream_cout;
    std::ostream* split_stream_cerr;

    std::ofstream* log_file;
  };


  template <typename LDEVC = LogDeviceConfig>
  class LogDevice : public BasicLogDevice
  {
  public:
    typedef LDEVC Config;

    LogDevice (std::ofstream& logfile);
    LogDevice ();
    ~LogDevice ();

    void SwitchLogFile (std::ofstream& logfile);
    void NoLogFile ();

    template <typename LDESC>
    std::ostream& SplitLog ();

    template <typename LDESC>
    std::ostream& SplitWarn ();

  protected:
    direction DetermineDirection (const bool to_std, const bool to_file);
  };


  template <typename LDESC, typename LDEVC>
  class LogDestination
  {
  public:
    typedef LogDevice <LDEVC> Device;
    typedef typename LogDevice <LDEVC>::Config DeviceConfig;

    LogDestination (std::string i_context, Device& i_device);

    inline bool DoLog ()  {return (!LDEVC::disabled) && (LDESC::echo_log_stdout || LDESC::log_to_file);}
    inline bool DoWarn () {return (!LDEVC::disabled) && (LDESC::echo_warn_stderr || LDESC::warn_to_file);}

    std::ostream& Log ();

    std::ostream& Warn ();

  protected:
    std::string context;
    Device& device;
  };


  template <typename LDESC, typename LDEVC, typename WL_TRAITS>
  class Logger
  {
  public:
    typedef LogDestination <LDESC, LDEVC> Destination;
    Logger (Destination& i_destination);
  
    inline bool DoLog () {return destination.DoLog () && WL_TRAITS::log; }
    inline bool DoWarn () {return destination.DoLog () && WL_TRAITS::warn; }

    std::ostream& Log (const char* pretty_function_name);
    std::ostream& Warn (const char* pretty_function_name);
  private:
    Destination& destination;
  };


  template <typename LDESC, typename LDEVC, typename WL_TRAITS, typename OBJ>
  class ObjectLogger : public Logger <LDESC, LDEVC, WL_TRAITS>
  { 
  public:
    typedef typename Logger <LDESC, LDEVC, WL_TRAITS>::Destination Destination;
    ObjectLogger (Destination& i_destination, OBJ* i_parent);
    ~ObjectLogger ();

    std::ostream& Log (const char* pretty_function_name);
    std::ostream& Warn (const char* pretty_function_name);
  private:
    OBJ* parent;
  };


  // we export cout and cerr to avoid <iostream> inclusion
  extern std::ostream& wrap_cout;
  extern std::ostream& wrap_cerr;

} // end namespace Utility


// now we do some nasty preprocessor stuff...

#ifdef Q_LOG
#  ifndef UTILITY__CUSTOM_Q_LOG__
#    warning "the Q_LOG macro is already defined"
#  endif
#else
#  define Q_LOG(logger)    if (logger.DoLog ()) logger.Log (__PRETTY_FUNCTION__)
#endif

#ifdef Q_WARN
#  ifndef UTILITY__CUSTOM_Q_WARN__
#    warning "the Q_WARN macro is already defined"
#  endif
#else
#  define Q_WARN(logger)   if (logger.DoWarn ()) logger.Warn (__PRETTY_FUNCTION__)
#endif

#define UTILITY__LOGGER_TMPL__
#include "template/Logger.tcc"
#undef UTILITY__LOGGER_TMPL__

#endif // UTILITY__LOGGER_HH__