⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logging.c

📁 GSM猫管理程序
💻 C
字号:
/*SMS Server Tools 3Copyright (C) Keijo Kasvihttp://smstools3.kekekasvi.com/Based on SMS Server Tools 2 from Stefan Fringshttp://www.meinemullemaus.de/This program is free software unless you got it under another license directlyfrom the author. You can redistribute it and/or modify it under the terms ofthe GNU General Public License as published by the Free Software Foundation.Either version 2 of the License, or (at your option) any later version.*/#include "logging.h"#include "extras.h"#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <syslog.h>#include <time.h>#include <fcntl.h>#include <unistd.h>int  Filehandle;int  Level;int openlogfile(char* filename,char* progname,int facility,int level){  Level=level;  if (filename==0 || filename[0]==0 || strcmp(filename,"syslog")==0 || strcmp(filename,"0")==0)  {    openlog(progname,LOG_CONS,facility);    Filehandle=-1;    return 0;  }  else if (strcmp(filename, "1") == 0 || strcmp(filename, "2") == 0) //(is_number(filename))  {    int oldfilehandle;    oldfilehandle=atoi(filename);    Filehandle=dup(oldfilehandle);    if (Filehandle<0)    {      fprintf(stderr,"cannot duplicate logfile handle\n");      exit(1);    }    else      return Filehandle;  }  else  {    Filehandle=open(filename,O_APPEND|O_WRONLY|O_CREAT,0640);    if (Filehandle<0)    {      fprintf(stderr,"cannot open logfile\n");      exit(1);    }    else      return Filehandle;  }}void closelogfile(){  if (Filehandle>=0)    close(Filehandle);}void writelogfile0(int severity, char *progname, char *text){  writelogfile(severity, progname, "%s", text);}void writelogfile(int severity,char* progname,char* format, ...){  va_list argp;  char text[2048];  char text2[1024];  char timestamp[40];  time_t now;  // make a string of the arguments  va_start(argp,format);  vsnprintf(text,sizeof(text),format,argp);  va_end(argp);  if (severity<=Level)  {    if (Filehandle<0)    {      if (strcmp(progname, "smsd") == 0)        syslog(severity, "%s", text);      else        syslog(severity, "%s: %s", progname, text);    }    else    {      time(&now);      strftime(timestamp,sizeof(timestamp),"%Y-%m-%d %H:%M:%S",localtime(&now));      snprintf(text2,sizeof(text2),"%s,%i, %s: %s\n",timestamp,severity,progname,text);      write(Filehandle,text2,strlen(text2));    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -