1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-24 15:04:20 +00:00
srs/trunk/src/kernel/srs_kernel_log.hpp

173 lines
6.1 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2018-01-07 02:58:53 +00:00
* Copyright (c) 2013-2018 Winlin
2017-03-25 09:21:39 +00:00
*
* 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.
*/
2013-11-23 03:36:07 +00:00
2014-03-01 02:53:28 +00:00
#ifndef SRS_KERNEL_LOG_HPP
#define SRS_KERNEL_LOG_HPP
2013-11-23 03:36:07 +00:00
#include <srs_core.hpp>
#include <stdio.h>
#include <errno.h>
#include <string.h>
2014-07-20 05:23:45 +00:00
#include <srs_kernel_consts.hpp>
/**
2017-03-25 09:21:39 +00:00
* 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.
*/
2017-03-26 02:16:21 +00:00
enum SrsLogLevel
{
2017-03-26 02:16:21 +00:00
SrsLogLevelForbidden = 0x00,
2017-03-25 09:21:39 +00:00
// only used for very verbose debug, generally,
// we compile without this level for high performance.
2017-03-26 02:16:21 +00:00
SrsLogLevelVerbose = 0x01,
SrsLogLevelInfo = 0x02,
SrsLogLevelTrace = 0x04,
SrsLogLevelWarn = 0x08,
SrsLogLevelError = 0x10,
SrsLogLevelDisabled = 0x20,
};
2014-03-01 10:06:20 +00:00
/**
2017-03-25 09:21:39 +00:00
* 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.
*/
2014-03-01 10:06:20 +00:00
class ISrsLog
2013-11-23 03:36:07 +00:00
{
public:
2014-03-01 10:06:20 +00:00
ISrsLog();
virtual ~ISrsLog();
public:
/**
2017-03-25 09:21:39 +00:00
* initialize log utilities.
*/
virtual srs_error_t initialize();
/**
* reopen the log file for log rotate.
*/
virtual void reopen();
2013-11-23 03:36:07 +00:00
public:
2014-03-01 10:06:20 +00:00
/**
2017-03-25 09:21:39 +00:00
* log for verbose, very verbose information.
*/
2014-03-01 10:06:20 +00:00
virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
/**
2017-03-25 09:21:39 +00:00
* log for debug, detail information.
*/
2014-03-01 10:06:20 +00:00
virtual void info(const char* tag, int context_id, const char* fmt, ...);
/**
2017-03-25 09:21:39 +00:00
* log for trace, important information.
*/
2014-03-01 10:06:20 +00:00
virtual void trace(const char* tag, int context_id, const char* fmt, ...);
/**
2017-03-25 09:21:39 +00:00
* log for warn, warn is something should take attention, but not a error.
*/
2014-03-01 10:06:20 +00:00
virtual void warn(const char* tag, int context_id, const char* fmt, ...);
/**
2017-03-25 09:21:39 +00:00
* log for error, something error occur, do something about the error, ie. close the connection,
* but we will donot abort the program.
*/
2014-03-01 10:06:20 +00:00
virtual void error(const char* tag, int context_id, const char* fmt, ...);
};
/**
* the context id manager to identify context, for instance, the green-thread.
* usage:
* _srs_context->generate_id(); // when thread start.
* _srs_context->get_id(); // get current generated id.
* int old_id = _srs_context->set_id(1000); // set context id if need to merge thread context.
*/
2014-03-01 10:06:20 +00:00
// the context for multiple clients.
class ISrsThreadContext
{
2013-11-23 03:36:07 +00:00
public:
2014-03-01 10:06:20 +00:00
ISrsThreadContext();
virtual ~ISrsThreadContext();
public:
/**
* generate the id for current context.
*/
2015-05-27 23:03:38 +00:00
virtual int generate_id();
/**
* get the generated id of current context.
*/
2014-03-01 10:06:20 +00:00
virtual int get_id();
/**
* set the id of current context.
* @return the previous id value; 0 if no context.
*/
virtual int set_id(int v);
2013-11-23 03:36:07 +00:00
};
2016-12-08 09:26:04 +00:00
// @global user must provides a log object
2014-03-01 10:06:20 +00:00
extern ISrsLog* _srs_log;
2016-12-08 09:26:04 +00:00
// @global user must implements the LogContext and define a global instance.
2014-03-01 10:06:20 +00:00
extern ISrsThreadContext* _srs_context;
2013-11-23 03:36:07 +00:00
// donot print method
2014-03-01 10:06:20 +00:00
#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__)
2017-02-03 06:57:28 +00:00
#endif
2013-11-23 03:36:07 +00:00
// use __FUNCTION__ to print c method
2017-02-03 06:57:28 +00:00
#if 0
2014-03-01 10:06:20 +00:00
#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__)
2017-02-03 06:57:28 +00:00
#endif
2013-11-23 03:36:07 +00:00
// use __PRETTY_FUNCTION__ to print c++ class:method
2017-02-03 06:57:28 +00:00
#if 0
2014-03-01 10:06:20 +00:00
#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__)
2013-11-23 03:36:07 +00:00
#endif
// TODO: FIXME: add more verbose and info logs.
#ifndef SRS_AUTO_VERBOSE
2014-03-18 03:32:58 +00:00
#undef srs_verbose
#define srs_verbose(msg, ...) (void)0
2013-11-23 03:36:07 +00:00
#endif
#ifndef SRS_AUTO_INFO
2014-03-18 03:32:58 +00:00
#undef srs_info
#define srs_info(msg, ...) (void)0
2013-11-23 03:36:07 +00:00
#endif
#ifndef SRS_AUTO_TRACE
2014-03-18 03:32:58 +00:00
#undef srs_trace
#define srs_trace(msg, ...) (void)0
2013-11-23 03:36:07 +00:00
#endif
#endif
2014-08-02 14:18:39 +00:00