Blame view

trunk/src/app/srs_app_log.cpp 7.2 KB
winlin authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

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.
*/
24
#include <srs_app_log.hpp>
winlin authored
25 26 27 28

#include <stdarg.h>
#include <sys/time.h>
29 30 31 32 33 34
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <srs_app_config.hpp>
winlin authored
35 36 37 38 39 40 41 42 43 44
SrsThreadContext::SrsThreadContext()
{
}

SrsThreadContext::~SrsThreadContext()
{
}

void SrsThreadContext::generate_id()
{
45
    static int id = 100;
winlin authored
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    cache[st_thread_self()] = id++;
}

int SrsThreadContext::get_id()
{
    return cache[st_thread_self()];
}

// the max size of a line of log.
#define LOG_MAX_SIZE 4096

// the tail append to each log.
#define LOG_TAIL '\n'
// reserved for the end of log data, it must be strlen(LOG_TAIL)
#define LOG_TAIL_SIZE 1

SrsFastLog::SrsFastLog()
{
64
    _level = SrsLogLevel::Trace;
winlin authored
65
    log_data = new char[LOG_MAX_SIZE];
66 67

    fd = -1;
68 69
    
    // TODO: support reload.
winlin authored
70 71 72 73 74
}

SrsFastLog::~SrsFastLog()
{
    srs_freepa(log_data);
75 76 77 78 79

    if (fd > 0) {
        ::close(fd);
        fd = -1;
    }
winlin authored
80 81
}
82 83 84 85 86 87 88 89 90 91
int SrsFastLog::level()
{
    return _level;
}

void SrsFastLog::set_level(int level)
{
    _level = level;
}
winlin authored
92 93
void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
{
94
    if (_level > SrsLogLevel::Verbose) {
winlin authored
95 96 97 98 99 100 101 102 103 104 105 106 107 108
        return;
    }
    
    int size = 0;
    if (!generate_header(tag, context_id, "verb", &size)) {
        return;
    }
    
    va_list ap;
    va_start(ap, fmt);
    // we reserved 1 bytes for the new line.
    size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
    va_end(ap);
109
    write_log(fd, log_data, size, SrsLogLevel::Verbose);
winlin authored
110 111 112 113
}

void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
{
114
    if (_level > SrsLogLevel::Info) {
winlin authored
115 116 117 118 119 120 121 122 123 124 125 126 127 128
        return;
    }
    
    int size = 0;
    if (!generate_header(tag, context_id, "debug", &size)) {
        return;
    }
    
    va_list ap;
    va_start(ap, fmt);
    // we reserved 1 bytes for the new line.
    size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
    va_end(ap);
129
    write_log(fd, log_data, size, SrsLogLevel::Info);
winlin authored
130 131 132 133
}

void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
{
134
    if (_level > SrsLogLevel::Trace) {
winlin authored
135 136 137 138 139 140 141 142 143 144 145 146 147 148
        return;
    }
    
    int size = 0;
    if (!generate_header(tag, context_id, "trace", &size)) {
        return;
    }
    
    va_list ap;
    va_start(ap, fmt);
    // we reserved 1 bytes for the new line.
    size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
    va_end(ap);
149
    write_log(fd, log_data, size, SrsLogLevel::Trace);
winlin authored
150 151 152 153
}

void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
{
154
    if (_level > SrsLogLevel::Warn) {
winlin authored
155 156 157 158 159 160 161 162 163 164 165 166 167 168
        return;
    }
    
    int size = 0;
    if (!generate_header(tag, context_id, "warn", &size)) {
        return;
    }
    
    va_list ap;
    va_start(ap, fmt);
    // we reserved 1 bytes for the new line.
    size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
    va_end(ap);
169
    write_log(fd, log_data, size, SrsLogLevel::Warn);
winlin authored
170 171 172 173
}

void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
{
174
    if (_level > SrsLogLevel::Error) {
winlin authored
175 176 177 178 179 180 181 182 183 184 185 186 187 188
        return;
    }
    
    int size = 0;
    if (!generate_header(tag, context_id, "error", &size)) {
        return;
    }
    
    va_list ap;
    va_start(ap, fmt);
    // we reserved 1 bytes for the new line.
    size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
    va_end(ap);
189 190 191
    // add strerror() to error msg.
    size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno));
192
    write_log(fd, log_data, size, SrsLogLevel::Error);
winlin authored
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
}

bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size)
{
    // clock time
    timeval tv;
    if (gettimeofday(&tv, NULL) == -1) {
        return false;
    }
    
    // to calendar time
    struct tm* tm;
    if ((tm = localtime(&tv.tv_sec)) == NULL) {
        return false;
    }
    
    // write log header
    int log_header_size = -1;
    
    if (tag) {
213 214 215 216
        log_header_size = snprintf(log_data, LOG_MAX_SIZE, 
            "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%d] ", 
            1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000), 
            level_name, tag, context_id, errno);
winlin authored
217
    } else {
218 219 220 221
        log_header_size = snprintf(log_data, LOG_MAX_SIZE, 
            "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%d] ", 
            1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000), 
            level_name, context_id, errno);
winlin authored
222 223 224 225 226 227 228 229 230 231 232 233
    }

    if (log_header_size == -1) {
        return false;
    }
    
    // write the header size.
    *header_size = srs_min(LOG_MAX_SIZE - 1, log_header_size);
    
    return true;
}
234
void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
winlin authored
235 236 237 238 239 240 241
{
    // ensure the tail and EOF of string
    //      LOG_TAIL_SIZE for the TAIL char.
    //      1 for the last char(0).
    size = srs_min(LOG_MAX_SIZE - 1 - LOG_TAIL_SIZE, size);
    
    // add some to the end of char.
242 243
    str_log[size++] = LOG_TAIL;
    str_log[size++] = 0;
winlin authored
244
    
245 246 247 248 249 250
    if (fd < 0 || !_srs_config->get_srs_log_tank_file()) {
        // if is error msg, then print color msg.
        // \033[31m : red text code in shell
        // \033[32m : green text code in shell
        // \033[33m : yellow text code in shell
        // \033[0m : normal text code
251
        if (level <= SrsLogLevel::Trace) {
252
            printf("%s", str_log);
253
        } else if (level == SrsLogLevel::Warn) {
254 255 256 257
            printf("\033[33m%s\033[0m", str_log);
        } else{
            printf("\033[31m%s\033[0m", str_log);
        }
258
    }
259 260
    
    // open log file. if specified
winlin authored
261 262 263 264 265 266 267 268 269 270
    if (!_srs_config->get_srs_log_file().empty() && fd < 0) {
        std::string filename = _srs_config->get_srs_log_file();
        
        fd = ::open(filename.c_str(), O_RDWR | O_APPEND);
        
        if(fd == -1 && errno == ENOENT) {
            fd = open(filename.c_str(), 
                O_RDWR | O_CREAT | O_TRUNC, 
                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
            );
271 272
        }
    }
273
    
winlin authored
274
    // write log to file.
275
    if (fd > 0 && _srs_config->get_srs_log_tank_file()) {
winlin authored
276 277
        ::write(fd, str_log, size);
    }
winlin authored
278
}