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

Annotation of /lbbs/src/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations)
Sun Mar 20 17:37:14 2005 UTC (21 years ago) by sysadm
Branch: MAIN
Changes since 1.8: +19 -19 lines
Content type: text/x-csrc
*** empty log message ***

1 sysadm 1.1 /***************************************************************************
2     log.c - description
3     -------------------
4     begin : Mon Oct 18 2004
5     copyright : (C) 2004 by leaf
6     email : leaflet@leafok.com
7     ***************************************************************************/
8    
9     /***************************************************************************
10     * *
11     * This program is free software; you can redistribute it and/or modify *
12     * it under the terms of the GNU General Public License as published by *
13     * the Free Software Foundation; either version 2 of the License, or *
14     * (at your option) any later version. *
15     * *
16     ***************************************************************************/
17    
18 sysadm 1.6 #include "io.h"
19     #include <stdio.h>
20     #include <stdarg.h>
21     #include <time.h>
22 sysadm 1.1
23     FILE *fp_log_std;
24     FILE *fp_log_err;
25    
26     int
27     log_begin (char *file_log_std, char *file_log_err)
28     {
29     fp_log_std = fopen (file_log_std, "a");
30     if (fp_log_std == NULL)
31     {
32     perror ("log_begin failed\n");
33     return -1;
34     }
35    
36     fp_log_err = fopen (file_log_err, "a");
37     if (fp_log_err == NULL)
38     {
39     perror ("log_begin failed\n");
40 sysadm 1.4 return -2;
41 sysadm 1.1 }
42    
43     return 0;
44     }
45    
46     int
47     log_end ()
48     {
49     fclose (fp_log_std);
50     fclose (fp_log_err);
51     }
52    
53     int
54 sysadm 1.2 log_head (char *buf)
55     {
56     time_t t;
57     char s_time[256];
58 sysadm 1.9 t = time (0);
59    
60     strftime (s_time, 256, "%Y-%m-%d %H:%M:%S", localtime (&t));
61     sprintf (buf, "[%s] [%d] ", s_time, getpid ());
62    
63 sysadm 1.2 return 0;
64     }
65    
66     int
67 sysadm 1.9 log_std (const char *format, ...)
68 sysadm 1.1 {
69 sysadm 1.6 va_list args;
70     int retval;
71 sysadm 1.2 char buf[1024];
72    
73 sysadm 1.9 log_head (buf);
74     strcat (buf, format);
75 sysadm 1.3
76 sysadm 1.6 va_start (args, format);
77 sysadm 1.7 retval = vfprintf (fp_log_std, buf, args);
78 sysadm 1.6 va_end (args);
79 sysadm 1.1
80 sysadm 1.9 fflush (fp_log_std);
81 sysadm 1.6
82     return retval;
83 sysadm 1.1 }
84    
85     int
86 sysadm 1.9 log_error (const char *format, ...)
87 sysadm 1.1 {
88 sysadm 1.6 va_list args;
89     int retval;
90 sysadm 1.2 char buf[1024];
91 sysadm 1.1
92 sysadm 1.9 log_head (buf);
93     strcat (buf, format);
94 sysadm 1.3
95 sysadm 1.6 va_start (args, format);
96 sysadm 1.7 retval = vfprintf (fp_log_err, buf, args);
97 sysadm 1.6 va_end (args);
98 sysadm 1.2
99 sysadm 1.9 fflush (fp_log_err);
100 sysadm 1.1
101 sysadm 1.6 return retval;
102 sysadm 1.1 }
103 sysadm 1.5
104     int
105 sysadm 1.9 log_std_redirect (int fd)
106 sysadm 1.5 {
107     int ret;
108 sysadm 1.9 close (fileno (fp_log_std));
109     ret = dup2 (fd, fileno (fp_log_std));
110 sysadm 1.5 return ret;
111     }
112    
113     int
114 sysadm 1.9 log_err_redirect (int fd)
115 sysadm 1.5 {
116     int ret;
117 sysadm 1.9 close (fileno (fp_log_err));
118     ret = dup2 (fd, fileno (fp_log_err));
119 sysadm 1.5 return ret;
120     }

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