File: log.cpp

package info (click to toggle)
libyami 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,152 kB
  • sloc: cpp: 44,247; ansic: 1,255; makefile: 728; lisp: 479; sh: 21; python: 19
file content (89 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/*  log.cpp is to enable debug log with LIBYAMI_LOG_LEVEL and LIBYAMI_TRACE:
 *  LIBYAMI_LOG_LEVEL=log_level: the record will be printed if the log_level larger than (Error, 0), (Warning, 1), (Info, 2)(Debug & Debug_, 3).
 *  LIBYAMI_LOG=log_file: the libyami log saved into log_file;
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
  
#include "log.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include "common/lock.h"

int yamiLogFlag;
FILE* yamiLogFn;
int isInit = 0;
static YamiMediaCodec::Lock g_traceLock;

void yamiTraceInit();

class TraceInitor {
  public:
    TraceInitor() { yamiTraceInit(); }
    ~TraceInitor() {}
};
static class TraceInitor g_initor;

void yamiTraceInit()
{
    YamiMediaCodec::AutoLock locker(g_traceLock);
    if(!isInit){
        char* libyamiLogLevel = getenv("LIBYAMI_LOG_LEVEL");
        char* libyamLog = getenv("LIBYAMI_LOG");
        FILE* tmp;

        yamiLogFn = stderr;
        if (libyamiLogLevel){
            yamiLogFlag = atoi(libyamiLogLevel);
            if (libyamLog){
                time_t now;
                struct tm* curtime;
                char filename[80];
                
                time(&now);

                if ((curtime = localtime(&now))) {
                    snprintf(filename, sizeof(filename), "%s_%2d_%02d_%02d_%02d_%02d", libyamLog,
                    	curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday,
                    	curtime->tm_hour, curtime->tm_sec);
                } else {
                    snprintf(filename, sizeof(filename), "%s", libyamLog);
                }

            	if ((tmp = fopen(filename, "w"))){
                	yamiLogFn = tmp;
                        ERROR("Libyami_Trace is on, save log into %s.\n", filename);
            	} else {
                        ERROR("Open file %s failed.\n", filename);
            	}
            }
        }
        else {
            yamiLogFlag = YAMI_LOG_ERROR;
        }
        isInit = 1;
    }
#ifndef __ENABLE_DEBUG__
     if (yamiLogFlag > YAMI_LOG_ERROR)
         fprintf(stderr, "yami log isn't enabled (--enable-debug)\n");
 #endif
}