Blame view

trunk/src/kernel/srs_kernel_log.hpp 5.4 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
winlin authored
24 25
#ifndef SRS_KERNEL_LOG_HPP
#define SRS_KERNEL_LOG_HPP
winlin authored
26 27

/*
28
#include <srs_kernel_log.hpp>
winlin authored
29 30 31 32 33 34 35 36 37
*/

#include <srs_core.hpp>

#include <stdio.h>

#include <errno.h>
#include <string.h>
38 39
#include <srs_kernel_consts.hpp>
winlin authored
40
/**
41 42 43 44 45 46 47 48 49 50 51 52 53 54
* the log level, for example:
* if specified Debug level, all level messages will be logged.
* if specified Warn level, only Warn/Error/Fatal level messages will be logged.
*/
class SrsLogLevel
{
public:
    // only used for very verbose debug, generally, 
    // we compile without this level for high performance.
    static const int Verbose = 0x01;
    static const int Info = 0x02;
    static const int Trace = 0x03;
    static const int Warn = 0x04;
    static const int Error = 0x05;
55 56
    // specified the disabled level, no log, for utest.
    static const int Disabled = 0x06;
57 58 59
};

/**
winlin authored
60 61 62 63 64
* the log interface provides method to write log.
* but we provides some macro, which enable us to disable the log when compile.
* @see also SmtDebug/SmtTrace/SmtWarn/SmtError which is corresponding to Debug/Trace/Warn/Fatal.
*/ 
class ISrsLog
winlin authored
65 66
{
public:
winlin authored
67 68
    ISrsLog();
    virtual ~ISrsLog();
winlin authored
69
public:
winlin authored
70
    /**
71 72 73 74 75
    * initialize log utilities.
    */
    virtual int initialize();
public:
    /**
winlin authored
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
    * log for verbose, very verbose information.
    */
    virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
    /**
    * log for debug, detail information.
    */
    virtual void info(const char* tag, int context_id, const char* fmt, ...);
    /**
    * log for trace, important information.
    */
    virtual void trace(const char* tag, int context_id, const char* fmt, ...);
    /**
    * log for warn, warn is something should take attention, but not a error.
    */
    virtual void warn(const char* tag, int context_id, const char* fmt, ...);
    /**
    * log for error, something error occur, do something about the error, ie. close the connection,
    * but we will donot abort the program.
    */
    virtual void error(const char* tag, int context_id, const char* fmt, ...);
};

// the context for multiple clients.
class ISrsThreadContext
{
winlin authored
101
public:
winlin authored
102 103 104 105 106
    ISrsThreadContext();
    virtual ~ISrsThreadContext();
public:
    virtual void generate_id();
    virtual int get_id();
winlin authored
107 108
};
winlin authored
109 110 111
// user must provides a log object
extern ISrsLog* _srs_log;
winlin authored
112
// user must implements the LogContext and define a global instance.
winlin authored
113
extern ISrsThreadContext* _srs_context;
winlin authored
114 115

// donot print method
winlin authored
116 117 118 119 120 121
#if 1
    #define srs_verbose(msg, ...) _srs_log->verbose(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_info(msg, ...)    _srs_log->info(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_trace(msg, ...)   _srs_log->trace(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_warn(msg, ...)    _srs_log->warn(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_error(msg, ...)   _srs_log->error(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
winlin authored
122
// use __FUNCTION__ to print c method
winlin authored
123 124 125 126 127 128
#elif 0
    #define srs_verbose(msg, ...) _srs_log->verbose(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_info(msg, ...)    _srs_log->info(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_trace(msg, ...)   _srs_log->trace(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_warn(msg, ...)    _srs_log->warn(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_error(msg, ...)   _srs_log->error(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
winlin authored
129 130
// use __PRETTY_FUNCTION__ to print c++ class:method
#else
winlin authored
131 132 133 134 135
    #define srs_verbose(msg, ...) _srs_log->verbose(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_info(msg, ...)    _srs_log->info(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_trace(msg, ...)   _srs_log->trace(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_warn(msg, ...)    _srs_log->warn(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
    #define srs_error(msg, ...)   _srs_log->error(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
winlin authored
136 137
#endif
138
// TODO: FIXME: add more verbose and info logs.
139
#ifndef SRS_AUTO_VERBOSE
140 141
    #undef srs_verbose
    #define srs_verbose(msg, ...) (void)0
winlin authored
142
#endif
143
#ifndef SRS_AUTO_INFO
144 145
    #undef srs_info
    #define srs_info(msg, ...) (void)0
winlin authored
146
#endif
147
#ifndef SRS_AUTO_TRACE
148 149
    #undef srs_trace
    #define srs_trace(msg, ...) (void)0
winlin authored
150 151 152
#endif

#endif
153