📄 log.cpp
字号:
/*
* Multimedia Multipoint Conferencing Unit(MMCU), ver 1.0
* Copyright (c) KCC-Eoun Information Center
*
* File: Log.cpp
* Description: define functions for log informations.
*
* The Initial Writer of Original Code is Yong Su.Han
*
* $Log: LOG.cpp, v $
* Revision 1.1 2006-08-28 Yong Su.Han
* Modified DAERYON_LOG_MSG function
*
* Revision 1.1 2006-07-18 Yong Su.Han
* Add log information
*
* Initial version 2006-05-18 Yong Su.Han
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <time.h>
#include <direct.h>
#include "Log.h"
static int g_nLogLevel = LOG_INFO;
static int g_bOutMode = 1; //0 : DebugWindow, 1 : File
static FILE *g_pClnFile = NULL;
static FILE *g_pRtspFile = NULL;
static FILE *g_pRtpFile = NULL;
static FILE *g_pIocpFile = NULL;
static char buf[LOG_BUF_SIZE];
static char Module[MODULE_NUMS][30] =
{
"HPlayer",
"RTP",
"RTSP",
"IOCP"
};
static char Log[9][10] =
{
"NONE",
"EMERG",
"ALERT",
"CRITICAL",
"ERROR",
"WARNING",
"NOTICE",
"INFO",
"DEBUG",
};
//get time of a day
int GetDayTime (struct timeval *t, char *strtime)
{
struct _timeb temp;
time_t secs;
_ftime(&temp);
t->tv_sec = temp.time;
t->tv_usec = temp.millitm;
time( &secs );
if( strtime )
{
strftime(strtime, 256, "%H:%M:%S", localtime(&secs));
}
return (0);
}
//setting global loglevel
void SetGlobalLogLevel(int nLogLevel)
{
if (nLogLevel > LOG_DEBUG || nLogLevel < 0) return;
g_nLogLevel = nLogLevel;
}
void GetCurDirectory(char *filepath, int len)
{
int pos;
char *cmdline, *dest;
cmdline = GetCommandLine();
if(cmdline[0] == '"')
cmdline++;
dest = strrchr(cmdline, '\\');
pos = dest - cmdline + 1;
strncpy(filepath, cmdline, pos);
filepath[pos] = 0;
return;
}
//open log file
void OpenLogFile(int nModuleID, const char *filename)
{
char logfname[MAX_PATH] = "";
char strtime[MAX_PATH] = "";
char dir[MAX_PATH] = "";
int pos;
time_t secs;
char *dest;
if( g_nLogLevel == LOG_NONE )
return;
GetCurDirectory(logfname, MAX_PATH);
strcat(logfname, filename);
dest = strrchr(logfname, '\\');
pos = dest - logfname;
strncpy(dir, logfname, pos);
dir[pos] = 0;
_mkdir(dir);
time( &secs );
strftime(strtime, 256, LOG_FORMAT, localtime(&secs));
sprintf(logfname, "%s-log-%s.log", logfname, strtime);
switch(nModuleID)
{
case HCLN:
g_pClnFile = fopen(logfname, "w");
break;
case RTP:
g_pRtpFile = fopen(logfname, "w");
break;
case RTSP:
g_pRtspFile = fopen(logfname, "w");
break;
case IOCP:
g_pIocpFile = fopen(logfname, "w");
break;
default:
break;
}
}
//flush log file
void FlushLogFile(void)
{
if( g_pClnFile )
fflush(g_pClnFile);
if( g_pRtpFile )
fflush(g_pRtpFile);
if( g_pRtspFile )
fflush(g_pRtspFile);
if( g_pIocpFile )
fflush(g_pIocpFile);
}
//close log file
void CloseLogFile(void)
{
if( g_pClnFile )
fclose(g_pClnFile);
if( g_pRtspFile )
fclose(g_pRtspFile);
if( g_pRtpFile )
fclose(g_pRtpFile);
if( g_pIocpFile )
fclose(g_pIocpFile);
}
/*
Function: LogMsg
Description :
log profile.
Return :
Parameter(s) :
level : Log level
fmt : print format
Author : HYS
Final update date : 2006-08-28
*/
void LogMsg(int loglevel, int module, const char *fmt, ...)
{
struct timeval thistime;
va_list args;
va_start(args, fmt);
if( loglevel > g_nLogLevel ) return;
if( g_bOutMode )
{
ModuleMessage(loglevel, module, fmt, args);
}
else
{
GetDayTime(&thistime, buf);
vsprintf(buf, fmt, args);
OutputDebugString(buf);
OutputDebugString("\n");
}
va_end(args);
FlushLogFile();
}
void ModuleMessage(int loglevel, int module, const char *fmt, va_list ap)
{
FILE *logfile = NULL;
struct timeval thistime;
if( loglevel > g_nLogLevel ) return;
switch(module)
{
case RTP:
logfile = g_pRtpFile;
break;
case RTSP:
logfile = g_pRtspFile;
break;
case HCLN:
logfile = g_pClnFile;
break;
case IOCP:
logfile = g_pIocpFile;
break;
}
GetDayTime(&thistime, buf);
if( logfile )
{
fprintf(logfile, "%s.%03lu-%s-%s:\t\t ", buf, thistime.tv_usec, Module[module], Log[loglevel]);
vfprintf(logfile, fmt, ap);
fprintf(logfile, "\n");
}
}
unsigned int GenerateRand()
{
UINT nRand;
srand(GetTickCount());
nRand = (rand() << 16) | rand();
return nRand;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -