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

139 lines
5.5 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2021-04-16 01:25:55 +00:00
* Copyright (c) 2013-2021 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>
2020-06-18 03:45:43 +00:00
#include <string>
2013-11-23 03:36:07 +00:00
2014-07-20 05:23:45 +00:00
#include <srs_kernel_consts.hpp>
2019-04-22 01:19:05 +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,
2019-04-22 01:19:05 +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,
};
2019-04-22 01:19:05 +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:
2019-04-22 01:19:05 +00:00
// Initialize log utilities.
2020-06-25 05:14:59 +00:00
virtual srs_error_t initialize() = 0;
2019-04-22 01:19:05 +00:00
// Reopen the log file for log rotate.
2020-06-25 05:14:59 +00:00
virtual void reopen() = 0;
2013-11-23 03:36:07 +00:00
public:
2019-04-22 01:19:05 +00:00
// The log for verbose, very verbose information.
virtual void verbose(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
2019-04-22 01:19:05 +00:00
// The log for debug, detail information.
virtual void info(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
2019-04-22 01:19:05 +00:00
// The log for trace, important information.
virtual void trace(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
2019-04-22 01:19:05 +00:00
// The log for warn, warn is something should take attention, but not a error.
virtual void warn(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
2019-04-22 01:19:05 +00:00
// The 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, SrsContextId context_id, const char* fmt, ...) = 0;
2014-03-01 10:06:20 +00:00
};
// The logic context, for example, a RTMP connection, or RTC Session, etc.
2020-06-25 05:14:59 +00:00
// We can grep the context id to identify the logic unit, for debugging.
// For example:
// SrsContextId cid = _srs_context->get_id(); // Get current context id.
// SrsContextId new_cid = _srs_context->generate_id(); // Generate a new context id.
// SrsContextId old_cid = _srs_context->set_id(new_cid); // Change the context id.
2020-06-25 05:14:59 +00:00
class ISrsContext
2014-03-01 10:06:20 +00:00
{
2013-11-23 03:36:07 +00:00
public:
2020-06-25 05:14:59 +00:00
ISrsContext();
virtual ~ISrsContext();
2014-03-01 10:06:20 +00:00
public:
// Generate a new context id.
// @remark We do not set to current thread, user should do this.
virtual SrsContextId generate_id() = 0;
// Get the context id of current thread.
2020-07-13 03:19:34 +00:00
virtual const SrsContextId& get_id() = 0;
// Set the context id of current thread.
2020-07-13 03:19:34 +00:00
// @return the current context id.
virtual const SrsContextId& set_id(const SrsContextId& v) = 0;
2013-11-23 03:36:07 +00:00
};
2019-04-22 01:19:05 +00:00
// @global User must provides a log object
2014-03-01 10:06:20 +00:00
extern ISrsLog* _srs_log;
2019-04-22 01:19:05 +00:00
// @global User must implements the LogContext and define a global instance.
2020-06-25 05:14:59 +00:00
extern ISrsContext* _srs_context;
2013-11-23 03:36:07 +00:00
2019-04-22 01:19:05 +00:00
// Log style.
// Use __FUNCTION__ to print c method
// Use __PRETTY_FUNCTION__ to print c++ class:method
2020-09-10 03:07:21 +00:00
#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__)
// With tag.
#define srs_verbose2(tag, msg, ...) _srs_log->verbose(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_info2(tag, msg, ...) _srs_log->info(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_trace2(tag, msg, ...) _srs_log->trace(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_warn2(tag, msg, ...) _srs_log->warn(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_error2(tag, msg, ...) _srs_log->error(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
2013-11-23 03:36:07 +00:00
2019-04-22 01:19:05 +00:00
// TODO: FIXME: Add more verbose and info logs.
#ifndef SRS_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_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_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