| // Copyright 2011 Google Inc. All Rights Reserved. |
| // Author: qianzhang@google.com (ken Zhang) |
| |
| #include <stdio.h> |
| #include <time.h> |
| #include <stdlib.h> |
| #include <stdlib.h> |
| #include <stdarg.h> |
| #include <string.h> |
| #include "sagelog.h" |
| |
| int sagelog_enabled = 1; |
| int console_enabled = 1; |
| int trace_level = 3; |
| char log_filename[128] = "/tmp/SageEncoder.log"; |
| |
| void _enable_native_log(void) |
| { |
| sagelog_enabled = 1; |
| } |
| |
| void _disable_native_log(void) |
| { |
| sagelog_enabled = 0; |
| } |
| |
| void _set_native_log(int level) |
| { |
| if (level == 0) { |
| sagelog_enabled = 0; |
| printf("sagelog is disabled."); |
| } else { |
| sagelog_enabled = 1; |
| trace_level = level; |
| printf("sagelog is enabled, \"%s\"\n", log_filename); |
| } |
| } |
| |
| void _sagelog(int type, int level, const char *cstr, ...) |
| { |
| time_t ct; |
| struct tm ltm; |
| va_list args; |
| char szInfo[1024 * 3]; |
| va_list va; |
| FILE *fp; |
| |
| if (!sagelog_enabled) |
| return; |
| if (type == _LOG_TRACE && level > trace_level) |
| return; |
| |
| fp = fopen(log_filename, "a"); |
| if (fp == NULL) |
| return; |
| time(&ct); |
| localtime_r(&ct, <m); |
| |
| va_start(va, cstr); |
| |
| snprintf(szInfo, sizeof(szInfo) - 3, TEXT("%02d/%02d/%d %02d:%02d:%02d "), |
| ltm.tm_mon + 1, ltm.tm_mday, ltm.tm_year + 1900, ltm.tm_hour, |
| ltm.tm_min, ltm.tm_sec); |
| |
| vsnprintf(szInfo + lstrlen(szInfo), (int)sizeof(szInfo) - lstrlen(szInfo) - 3, |
| cstr, va); |
| lstrcat(szInfo, TEXT("\r\n")); |
| fwrite((char *)szInfo, 1, lstrlen(szInfo), fp); |
| |
| if (console_enabled) |
| printf("%s", szInfo); |
| |
| va_end(args); |
| fclose(fp); |
| } |
| |
| void _flog_setup(char *filename, int enable) |
| { |
| if (filename != NULL && filename[0]) |
| strncpy(log_filename, filename, sizeof(log_filename)); |
| |
| sagelog_enabled = enable; |
| } |
| |
| char *_get_flog_path(void) |
| { |
| return log_filename; |
| } |
| |
| int _get_flog_state(void) |
| { |
| return sagelog_enabled; |
| } |
| |
| int _flog_check(void) |
| { |
| FILE *fp = fopen("NATIVE_LOG.ENABLE", "r"); |
| if (fp != NULL) { |
| sagelog_enabled = 1; |
| fclose(fp); |
| return 1; |
| } |
| return 0; |
| } |