[C][Linux] logi systemowe

0

Witam, chciałbym się dowiedzieć w jaki sposób mój daemon może dopisywać swoje akcje do logów systemowych - czy robi się to poprzez innego daemona, czy tez po prostu wpisuje się recznie do wlasnego pliku w katalogu /var/log - bodajże?
Jak to się robi w profesjonalnych projektach?

0
#include <syslog.h>
#include <stdarg.h>
#include <stdio.h>
// i pewnie inne

#define LOGTYPE        2        /* 0 - nolog, 1 - syslog, 2 - filelog*/

pid_t pid;                               // pid procesu serwera
FILE* log=0;                           // no comment
char logfile[256];                     // sciezka do pliku logowania
char hostname[128];                    // nazwa komputera

void(*psyslog)(int,const char*,...);   // to sa 3 wskazniki, poprzez nie wywoluje sie 3 rozne grupy funkcji logujacych
void(*popenlog)(const char*,int,int);
void(*pcloselog)();

void nopenlog(const char*,int,int){
  // must be empty for no-logging
}

void ncloselog(){
  // must be empty for no-logging
}

void nsyslog(int,const char*,...){
  // must be empty for no-logging
}

void fopenlog(const char* name,int option,int facility){
  if(strcmp(name,"-")){
    log=fopen(name,"at");
  }else if(strcmp(name,"+")){
    log=fdopen(STDOUT_FILENO,"w");
  }log=fdopen(STDERR_FILENO,"w");
  if(!log){
    popenlog=openlog;
    pcloselog=closelog;
    psyslog=syslog;
    openlog(pname,option,facility);
    int err=errno;
    fprintf(stderr,"cannot open logfile (%s), redirecting to syslog: error %d [%s]\n",name,err,strerror(err));
    syslog(LOG_ERR,"cannot open logfile (%s), redirecting to syslog: error %d [%s]",name,err,strerror(err));
  }
}

void fcloselog(){
  int fd=fileno(log);
  if(fd!=1 && fd!=2)fclose(log);
}

void fsyslog(int,const char* frmt,...){
  va_list ap;
  va_start(ap,frmt);
  char buf[512];
  time_t t;
  time(&t);
  int l=strftime(buf,100,"%b %d %H:%M:%S ",localtime(&t));
  snprintf(buf+l,511-l,"%s %s[%u]: ",hostname,pname,pid);
  l=strlen(buf);
  vsnprintf(buf+l,511-l,frmt,ap);
  buf[511]=0;
  char* s=buf+l;
  while(*s){
    if(*s<' ' || *s>=127)*s='?';
    s++;
  }
  fprintf(log,"%s\n",buf);
#ifdef DEBUG
  fprintf(stderr,"%s\n",buf);
#endif
  fflush(log);
  va_end(ap);
}

int main(int argc,char** argv){

  pname=strrchr(*argv,'/');
  if(!(pname++))pname=*argv;
  gethostname(hostname,128);

#if LOGTYPE==0
  popenlog =nopenlog;
  pcloselog=ncloselog;
  psyslog  =nsyslog;
#elif LOGTYPE==1
  strcpy(logfile,pname);
  popenlog =openlog;
  pcloselog=closelog;
  psyslog  =syslog;
#elif LOGTYPE==2
  strcat(strcpy(logfile,"/var/log/"),pname);
  popenlog =fopenlog;
  pcloselog=fcloselog;
  psyslog  =fsyslog;
#endif

  char* sopt="snl:";
  option lopt[]={
    {"syslog",     0,0,'s'},
    {"nolog",      0,0,'n'},
    {"logfile",    1,0,'l'},
    {0,0,0,0}
  };

  int i;
  while((i=getopt_long(argc,argv,sopt,lopt,0))!=-1){
    switch(i){
      case 's':strcpy(logfile,pname);
               popenlog=openlog;
               pcloselog=closelog;
               psyslog=syslog;
               break;
      case 'n':popenlog=nopenlog;
               pcloselog=ncloselog;
               psyslog=nsyslog;
               break;
      case 'l':strcpy(logfile,optarg);
               popenlog=fopenlog;
               pcloselog=fcloselog;
               psyslog=fsyslog;
               break;
      default :return 0;
    };
  }

  pid=getpid();
  popenlog(logfile,LOG_NDELAY|LOG_NOWAIT|LOG_PID,LOG_USER);

  psyslog(LOG_NOTICE,"hello world");
  // ...

  pcloselog();
  return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1