1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "jsvc.h"
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <time.h>
21 
22 /* Wether debug is enabled or not */
23 bool log_debug_flag = false;
24 
25 /* Wether SYSLOG logging (for stderr) is enable or not. */
26 bool log_stderr_syslog_flag = false;
27 
28 /* Wether SYSLOG logging (for stdout) is enable or not. */
29 bool log_stdout_syslog_flag = false;
30 
31 /* The name of the jsvc binary. */
32 char *log_prog = "jsvc";
33 
34 /* Dump a debug trace message to stderr */
log_debug(const char * fmt,...)35 void log_debug(const char *fmt, ...)
36 {
37     va_list ap;
38     time_t now;
39     struct tm *nowtm;
40     char buff[80];
41 
42     if (log_debug_flag == false)
43         return;
44     if (fmt == NULL)
45         return;
46 
47     now = time(NULL);
48     nowtm = localtime(&now);
49     strftime(buff, sizeof(buff), "%Y-%m-%d %T", nowtm);
50     va_start(ap, fmt);
51     if (log_stderr_syslog_flag)
52         fprintf(stderr, "%s %d %s debug: ", buff, getpid(), log_prog);
53 #if defined(DEBUG) || defined(_DEBUG)
54     else
55         fprintf(stderr, "[debug] %s %d ", buff, getpid());
56 #endif
57     vfprintf(stderr, fmt, ap);
58     fprintf(stderr, "\n");
59     fflush(stderr);
60     va_end(ap);
61 }
62 
63 /* Dump an error message to stderr */
log_error(const char * fmt,...)64 void log_error(const char *fmt, ...)
65 {
66     va_list ap;
67     time_t now;
68     struct tm *nowtm;
69     char buff[80];
70 
71     if (fmt == NULL)
72         return;
73 
74     va_start(ap, fmt);
75     if (log_stderr_syslog_flag) {
76         now = time(NULL);
77         nowtm = localtime(&now);
78         strftime(buff, sizeof(buff), "%Y-%m-%d %T", nowtm);
79         fprintf(stderr, "%s %d %s error: ", buff, getpid(), log_prog);
80     }
81     vfprintf(stderr, fmt, ap);
82     fprintf(stderr, "\n");
83     fflush(stderr);
84     va_end(ap);
85 }
86