[svn-inject] Installing original source of suphp
[manu/suphp.git] / src / log.c
1 /*
2     suPHP - (c)2002-2004 Sebastian Marsching <sebastian@marsching.com>
3     
4     This file is part of suPHP.
5
6     suPHP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     suPHP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with suPHP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <time.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include "suphp.h"
31
32 int log_fd;
33 int log_initialized = 0;
34
35 void suphp_log_error(char *err_msg, ...)
36 {
37  char msg[4096];
38  va_list vargs;
39  
40  
41  // Build error message
42  va_start(vargs, err_msg);
43  vsnprintf(msg, 4095, err_msg, vargs);
44  va_end(vargs); 
45  
46  // Write message to logfile
47  suphp_log(msg, "error");
48 }
49
50 void suphp_log_info(char *info_msg, ...)
51 {
52  char msg[4096];
53  va_list vargs;
54  
55  
56  // Build error message
57  va_start(vargs, info_msg);
58  vsnprintf(msg, 4095, info_msg, vargs);
59  va_end(vargs); 
60  
61  // Write message to logfile
62  suphp_log(msg, "info");
63 }
64
65 void suphp_init_log()
66 {
67  if ((log_fd = open(OPT_LOGFILE, O_WRONLY | O_CREAT | O_APPEND | O_NOCTTY, S_IRUSR | S_IWUSR)) == -1)
68   error_sysmsg_exit(ERRCODE_NO_LOG, "Could not open logfile", __FILE__, __LINE__);
69  if (fcntl(log_fd, F_SETFD, FD_CLOEXEC))
70   error_sysmsg_exit(ERRCODE_UNKNOWN, "Could not set close-on-exec attribute for logfile", __FILE__, __LINE__);
71  log_initialized = 1;
72 }
73
74 void suphp_log(char *msg, char *category)
75 {
76  time_t ts;
77  struct tm *now;
78  char str_time[1024];
79  char row[8192];
80  
81  
82  // Get time
83  ts = time(NULL);
84  now = localtime(&ts);
85  strftime(str_time, 1023, "[%a %b %d %H:%M:%S %Y]", now);
86  
87  // Build entry for logfile
88  snprintf(row, 8191, "%s [%s] %s\n", str_time, category, msg);
89  
90  // Write to logfile
91  write(log_fd, row, strlen(row));
92 }