houxiao
2017-02-28 b4c22313c0ba28bb4b4f4dad4f0a28c2161cf6d2
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
/*
 * logger.hpp
 *
 *
 * Logger Library Header
 *
 *
 * Copyright (C) 2013-2017  Bryant Moscon - bmoscon@gmail.com
 *
 * Please see the LICENSE file for the terms and conditions 
 * associated with this software.
 *
 */
 
#ifndef __LOGGER__
#define __LOGGER__
 
 
#include <fstream>
#include <cassert>
#include <ctime>
#include <sstream>
 
 
// Log levels
typedef enum {
  VERBOSE = 0,
  DEBUG,
  INFO,
  NOTICE,
  WARNING,
  ERROR,
  CRITICAL
} logger_level;
 
 
 
class Logger : public std::ostringstream {
public:
    Logger(std::ostream& s);
    Logger(const char *f);
    Logger(const std::string& f);
    Logger (const Logger &) = delete;
    Logger &operator= (const Logger &) = delete;
    ~Logger();
    
    
    void set_level(const logger_level& level);
    void set_default_line_level(const logger_level& level);
    void flush();
    template <typename T>
    Logger& operator<<(const T& t)
    {
    *static_cast<std::ostringstream *>(this) << t;
    return (*this);
    }
    
    Logger& operator<<(const logger_level& level);
    typedef Logger& (* LoggerManip)(Logger&);
    Logger& operator<<(LoggerManip m);
    
private:
    std::string get_time() const;
    inline const char* level_str(const logger_level& level);
    
    std::ofstream  _file;
    std::ostream&  _log; 
    logger_level   _level;
    logger_level   _line_level;
    logger_level   _default_line_level;
};
 
 
namespace std { 
    inline Logger& endl(Logger& out) 
    { 
    out.put('\n'); 
    out.flush(); 
    return (out); 
    } 
}
 
 
#endif