A function log of writing log log

xiaoxiao2021-03-06  42

#include #include #include #include #include #include #include

/ *** user deflude file: *** /

/ / # include #include "log_def.h"

Extern int GET_CONF (Char * Name, Char * Value);

/ ************************************************** ******************************* * Module Description: Common Function Set * // * includes functions: * // * Void Get_date_time () Take the date and time of the operating system * // * int WRITE_LOG () Normal, error log, or output to the screen * // ******************* *********************************************************** ********* /

/ ************************************************** ***************************** / / * function name: void get_date_time () * // * function: take the operating system Date and time * // * Enter output: char * Today output parameters, return date * // * char * Now_TIME output parameters, return time * / * / * // ************ *********************************************************** *************** / VOID _GET_DATE_TIME (CHAR * TODAY, CHAR * NOW_TIME) {structTM * TP; TIME_T STCLK;

TIME (& Stclk); TP = localtime (& stclk); sprintf (Today, "% 4D% 02D% 02D", TP-> TM_YEAR 1900, TP-> TM_MON 1, TP-> TM_MDAY); * (Today 8 ) = '/ 0'; sprintf (now_time, "% 02d% 02D% 02d", TP-> TM_HOUR, TP-> TM_MIN, TP-> TM_SEC); * (now_time 6) = '/ 0';} / *********************************************************** ***************************** / / * function name: int Write_log () * // * function: note or output to On-screen * // * Input Output: int il_flag Enter parameters, note log logo * // * 1: Normal log, -1: error log, 0: screen output * // * char * PFormat input parameters, format input parameters * // * Function Return: 0: Normal <0: Error * // * Call function: _GET_DATE_TIME * / / * Environment Variable: Run_Home * / / ***************** *********************************************************** ********** / i NT WRITE_LOG (int il_flag, const char * pformat, ...) {va_list ap_list; file * fp; char chtoday [9], chnow_time [7], chfilename [80]; char pchfilepath [60], chmvname [80]; Struct stat stfilestat;

_GET_DATE_TIME (ChToday, Chnow_time); Memset (chfilename, 0x00, sizeof (chfilename)); // get_conf ("syslog", pchfilepath); sprintf (pchfilepath, "/ root / test");

Switch (Ilog_Flag) {Case Normal_log: Sprintf (ChfileName, "% S / Log% 2.2s.dat", PCHFILEPATH, ChTODAY 6); Sprintf (chmvname, "% s / log% 2.2s.bak", PCHFILEPATH, ChTODAY 6); Break; Case Error_log: Sprintf (ChfileName, "% S / Err% S.dat", PCHFILEPATH, ChTODAY); // Sprintf (chfilename, "% S / Err% 2.2s.dat", PCHFILEPATH, ChTODAY ); Sprintf (chmvname, "% S / ERR% 2.2s.bak", pchfilepath, chtoday 6); Break; Case Scrout_log: / * Screen output * / Break; default: return (error_param_err);} / * Control errors Log file, tracking log file size is 1M * / if (iLog_flag == Normal_Log || Ilog_Flag == Error_Log) {if (st (chfilename, & stfilestat) == 0) {/ * file already existing * / if (stfilestat.st_size > = 1024 * 1024) {rename (chfilename, chmvname); }}}

IF (iLog_flag! = scout_log) {fp = fopen (chfilename, "a"); if (fp == (file *) null) {Write_log (Scrout_log, "Open file [% s] error! / n", chfilename) PERROR ("Open file"); return (error_open_file_err);}}

转载请注明原文地址:https://www.9cbs.com/read-63103.html

New Post(0)