/[LeafOK_CVS]/lbbs/src/log.c
ViewVC logotype

Diff of /lbbs/src/log.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.9 by sysadm, Sun Mar 20 17:37:14 2005 UTC Revision 1.13 by sysadm, Sun May 4 14:54:55 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            log.c  -  description                                                    log.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 18 2004          begin                : Mon Oct 18 2004
5      copyright            : (C) 2004 by leaf          copyright            : (C) 2004 by leaf
6      email                : leaflet@leafok.com          email                : leaflet@leafok.com
7   ***************************************************************************/   ***************************************************************************/
8    
9  /***************************************************************************  /***************************************************************************
# Line 17  Line 17 
17    
18  #include "io.h"  #include "io.h"
19  #include <stdio.h>  #include <stdio.h>
20    #include <string.h>
21  #include <stdarg.h>  #include <stdarg.h>
22    #include <sys/types.h>
23  #include <time.h>  #include <time.h>
24    #include <unistd.h>
25    
26  FILE *fp_log_std;  FILE *fp_log_std;
27  FILE *fp_log_err;  FILE *fp_log_err;
28    
29  int  int log_begin(char *file_log_std, char *file_log_err)
 log_begin (char *file_log_std, char *file_log_err)  
30  {  {
31    fp_log_std = fopen (file_log_std, "a");          fp_log_std = fopen(file_log_std, "a");
32    if (fp_log_std == NULL)          if (fp_log_std == NULL)
33      {          {
34        perror ("log_begin failed\n");                  perror("log_begin failed\n");
35        return -1;                  return -1;
36      }          }
37    
38    fp_log_err = fopen (file_log_err, "a");          fp_log_err = fopen(file_log_err, "a");
39    if (fp_log_err == NULL)          if (fp_log_err == NULL)
40      {          {
41        perror ("log_begin failed\n");                  perror("log_begin failed\n");
42        return -2;                  return -2;
43      }          }
44    
45    return 0;          return 0;
46  }  }
47    
48  int  void log_end()
 log_end ()  
49  {  {
50    fclose (fp_log_std);          fclose(fp_log_std);
51    fclose (fp_log_err);          fclose(fp_log_err);
52  }  }
53    
54  int  int log_head(char *buf)
 log_head (char *buf)  
55  {  {
56    time_t t;          time_t t;
57    char s_time[256];          char s_time[256];
58    t = time (0);          t = time(0);
59    
60    strftime (s_time, 256, "%Y-%m-%d %H:%M:%S", localtime (&t));          strftime(s_time, 256, "%Y-%m-%d %H:%M:%S", localtime(&t));
61    sprintf (buf, "[%s] [%d] ", s_time, getpid ());          sprintf(buf, "[%s] [%d] ", s_time, getpid());
62    
63    return 0;          return 0;
64  }  }
65    
66  int  int log_std(const char *format, ...)
 log_std (const char *format, ...)  
67  {  {
68    va_list args;          va_list args;
69    int retval;          int retval;
70    char buf[1024];          char buf[1024];
71    
72    log_head (buf);          log_head(buf);
73    strcat (buf, format);          strcat(buf, format);
74    
75    va_start (args, format);          va_start(args, format);
76    retval = vfprintf (fp_log_std, buf, args);          retval = vfprintf(fp_log_std, buf, args);
77    va_end (args);          va_end(args);
78    
79    fflush (fp_log_std);          fflush(fp_log_std);
80    
81    return retval;          return retval;
82  }  }
83    
84  int  int log_error(const char *format, ...)
 log_error (const char *format, ...)  
85  {  {
86    va_list args;          va_list args;
87    int retval;          int retval;
88    char buf[1024];          char buf[1024];
89    
90    log_head (buf);          log_head(buf);
91    strcat (buf, format);          strcat(buf, format);
92    
93    va_start (args, format);          va_start(args, format);
94    retval = vfprintf (fp_log_err, buf, args);          retval = vfprintf(fp_log_err, buf, args);
95    va_end (args);          va_end(args);
96    
97    fflush (fp_log_err);          fflush(fp_log_err);
98    
99    return retval;          return retval;
100  }  }
101    
102  int  int log_std_redirect(int fd)
 log_std_redirect (int fd)  
103  {  {
104    int ret;          int ret;
105    close (fileno (fp_log_std));          close(fileno(fp_log_std));
106    ret = dup2 (fd, fileno (fp_log_std));          ret = dup2(fd, fileno(fp_log_std));
107    return ret;          return ret;
108  }  }
109    
110  int  int log_err_redirect(int fd)
 log_err_redirect (int fd)  
111  {  {
112    int ret;          int ret;
113    close (fileno (fp_log_err));          close(fileno(fp_log_err));
114    ret = dup2 (fd, fileno (fp_log_err));          ret = dup2(fd, fileno(fp_log_err));
115    return ret;          return ret;
116  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1