Blame view

trunk/src/app/srs_app_log.cpp 9.3 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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <srs_app_config.hpp>
34
#include <srs_kernel_error.hpp>
35
#include <srs_app_utility.hpp>
36
#include <srs_kernel_utility.hpp>
37
winlin authored
38 39 40 41 42 43 44 45 46 47
SrsThreadContext::SrsThreadContext()
{
}

SrsThreadContext::~SrsThreadContext()
{
}

void SrsThreadContext::generate_id()
{
48
    static int id = 100;
winlin authored
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    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()
{
67
    _level = SrsLogLevel::Trace;
winlin authored
68
    log_data = new char[LOG_MAX_SIZE];
69 70

    fd = -1;
71
    log_to_file_tank = false;
winlin authored
72 73 74 75
}

SrsFastLog::~SrsFastLog()
{
76
    srs_freep(log_data);
77 78 79 80 81

    if (fd > 0) {
        ::close(fd);
        fd = -1;
    }
winlin authored
82
83 84 85
    if (_srs_config) {
        _srs_config->unsubscribe(this);
    }
86 87
}
88
int SrsFastLog::initialize()
89
{
90 91
    int ret = ERROR_SUCCESS;
    
92 93 94 95 96 97
    if (_srs_config) {
        _srs_config->subscribe(this);
    
        log_to_file_tank = _srs_config->get_log_tank_file();
        _level = srs_get_log_level(_srs_config->get_log_level());
    }
98 99
    
    return ret;
100 101
}
winlin authored
102 103
void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
{
104
    if (_level > SrsLogLevel::Verbose) {
winlin authored
105 106 107 108
        return;
    }
    
    int size = 0;
109
    if (!generate_header(false, tag, context_id, "verb", &size)) {
winlin authored
110 111 112 113 114 115 116 117 118
        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);
119
    write_log(fd, log_data, size, SrsLogLevel::Verbose);
winlin authored
120 121 122 123
}

void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
{
124
    if (_level > SrsLogLevel::Info) {
winlin authored
125 126 127 128
        return;
    }
    
    int size = 0;
129
    if (!generate_header(false, tag, context_id, "debug", &size)) {
winlin authored
130 131 132 133 134 135 136 137 138
        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);
139
    write_log(fd, log_data, size, SrsLogLevel::Info);
winlin authored
140 141 142 143
}

void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
{
144
    if (_level > SrsLogLevel::Trace) {
winlin authored
145 146 147 148
        return;
    }
    
    int size = 0;
149
    if (!generate_header(false, tag, context_id, "trace", &size)) {
winlin authored
150 151 152 153 154 155 156 157 158
        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);
159
    write_log(fd, log_data, size, SrsLogLevel::Trace);
winlin authored
160 161 162 163
}

void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
{
164
    if (_level > SrsLogLevel::Warn) {
winlin authored
165 166 167 168
        return;
    }
    
    int size = 0;
169
    if (!generate_header(true, tag, context_id, "warn", &size)) {
winlin authored
170 171 172 173 174 175 176 177 178
        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);
179
    write_log(fd, log_data, size, SrsLogLevel::Warn);
winlin authored
180 181 182 183
}

void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
{
184
    if (_level > SrsLogLevel::Error) {
winlin authored
185 186 187 188
        return;
    }
    
    int size = 0;
189
    if (!generate_header(true, tag, context_id, "error", &size)) {
winlin authored
190 191 192 193 194 195 196 197 198
        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);
199
    // add strerror() to error msg.
200 201 202
    if (errno != 0) {
        size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno));
    }
203
204
    write_log(fd, log_data, size, SrsLogLevel::Error);
winlin authored
205 206
}
207 208 209
int SrsFastLog::on_reload_log_tank()
{
    int ret = ERROR_SUCCESS;
210 211 212 213
    
    if (!_srs_config) {
        return ret;
    }
214 215

    bool tank = log_to_file_tank;
216
    log_to_file_tank = _srs_config->get_log_tank_file();
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

    if (tank) {
        return ret;
    }

    if (!log_to_file_tank) {
        return ret;
    }

    if (fd > 0) {
        ::close(fd);
    }
    open_log_file();
    
    return ret;
}

int SrsFastLog::on_reload_log_level()
{
    int ret = ERROR_SUCCESS;
    
238 239 240 241
    if (!_srs_config) {
        return ret;
    }
    
242
    _level = srs_get_log_level(_srs_config->get_log_level());
243 244 245 246 247 248 249
    
    return ret;
}

int SrsFastLog::on_reload_log_file()
{
    int ret = ERROR_SUCCESS;
250 251 252 253
    
    if (!_srs_config) {
        return ret;
    }
254 255 256 257 258 259 260 261 262 263 264 265 266

    if (!log_to_file_tank) {
        return ret;
    }

    if (fd > 0) {
        ::close(fd);
    }
    open_log_file();
    
    return ret;
}
267
bool SrsFastLog::generate_header(bool error, const char* tag, int context_id, const char* level_name, int* header_size)
winlin authored
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
{
    // 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;
    
284 285 286
    if (error) {
        if (tag) {
            log_header_size = snprintf(log_data, LOG_MAX_SIZE, 
287
                "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%d][%d] ", 
288
                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), 
289
                level_name, tag, getpid(), context_id, errno);
290 291
        } else {
            log_header_size = snprintf(log_data, LOG_MAX_SIZE, 
292
                "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%d][%d] ", 
293
                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), 
294
                level_name, getpid(), context_id, errno);
295
        }
winlin authored
296
    } else {
297 298
        if (tag) {
            log_header_size = snprintf(log_data, LOG_MAX_SIZE, 
299
                "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%d] ", 
300
                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), 
301
                level_name, tag, getpid(), context_id);
302 303
        } else {
            log_header_size = snprintf(log_data, LOG_MAX_SIZE, 
304
                "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%d] ", 
305
                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), 
306
                level_name, getpid(), context_id);
307
        }
winlin authored
308 309 310 311 312 313 314 315 316 317 318 319
    }

    if (log_header_size == -1) {
        return false;
    }
    
    // write the header size.
    *header_size = srs_min(LOG_MAX_SIZE - 1, log_header_size);
    
    return true;
}
320
void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
winlin authored
321 322 323 324 325 326 327
{
    // 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.
328 329
    str_log[size++] = LOG_TAIL;
    str_log[size++] = 0;
winlin authored
330
    
331 332
    // if not to file, to console and return.
    if (!log_to_file_tank) {
333 334 335 336 337
        // 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
338
        if (level <= SrsLogLevel::Trace) {
339
            printf("%s", str_log);
340
        } else if (level == SrsLogLevel::Warn) {
341 342 343 344
            printf("\033[33m%s\033[0m", str_log);
        } else{
            printf("\033[31m%s\033[0m", str_log);
        }
345 346

        return;
347
    }
348 349
    
    // open log file. if specified
350 351
    if (fd < 0) {
        open_log_file();
352
    }
353
    
winlin authored
354
    // write log to file.
355
    if (fd > 0) {
winlin authored
356 357
        ::write(fd, str_log, size);
    }
winlin authored
358
}
359 360 361

void SrsFastLog::open_log_file()
{
362 363 364 365
    if (!_srs_config) {
        return;
    }
    
366
    std::string filename = _srs_config->get_log_file();
367 368 369 370 371 372 373 374 375 376 377 378 379 380
    
    if (filename.empty()) {
        return;
    }
    
    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
        );
    }
}
381