3 #include <sys/socket.h>
4 #include <netinet/in.h>
10 #include <arpa/inet.h>
16 #if defined(__linux__) || defined(__APPLE__)
29 volatile int tonkoLogLevel = 2 ;
33 #ifdef RTS_LOG_DEFAULT_NET
34 static int output_flag = RTS_LOG_NET ;
36 static int output_flag = RTS_LOG_STDERR ;
39 static const char *getCmd(
void) ;
40 static int odesc = -1 ;
41 static int handchange ;
42 static FILE *fdesc = 0 ;
46 static char servER[80] =
"130.199.20.88" ;
48 static char servER[80] = RTS_LOG_HOST ;
51 static int port = RTS_LOG_PORT ;
53 static char cmd[1024] ;
70 int rtsLogOutput(
int flag)
76 void rtsLogAddCmd(
const char *cmd_in)
78 strncpy(cmd,cmd_in,
sizeof(cmd)-1) ;
82 int rtsLogAddDest(
const char *host,
int newport)
88 strncpy(servER,host,
sizeof(servER)-1) ;
92 if(odesc >= 0) close(odesc) ;
99 int rtsLogAddFile(
char *fname)
101 if(fdesc) fclose(fdesc) ;
105 fdesc = fopen(fname,
"w") ;
115 static char _g_fname[256];
116 static char *jml_fname = NULL;
117 void rtsLogAddJmlFile (
char *fname)
119 strcpy(_g_fname, fname);
120 jml_fname = _g_fname;
123 int rtsLogUnix_v(
const char *str, ...)
126 static int sockAddrSize ;
127 static struct sockaddr_in serverAddr ;
128 static const char *cmd_l ;
132 char *
string = buffer ;
133 unsigned int *cou = (
unsigned int *)buffer ;
142 if((output_flag & RTS_LOG_NET) && (odesc<0)) {
148 if(!handchange && (rts_host=getenv(
"RTS_LOG_HOST"))) {
149 strncpy(servER,rts_host,
sizeof(servER)-1) ;
155 sockAddrSize =
sizeof(
struct sockaddr_in) ;
156 memset((
char *)&serverAddr,0,sockAddrSize) ;
157 serverAddr.sin_family = AF_INET ;
158 serverAddr.sin_port = htons(port) ;
161 if((serverAddr.sin_addr.s_addr = inet_addr(servER)) == (
unsigned int)-1) {
166 odesc = socket(AF_INET, SOCK_DGRAM,0) ;
172 bufsize = 8*1024*1024 ;
174 ret = setsockopt(odesc,SOL_SOCKET,SO_SNDBUF,(
char *)&bufsize,
sizeof(bufsize)) ;
186 if(strncmp(str,
"COLOR",5)==0) {
190 if(strncmp(str,DBG,strlen(DBG))==0) {
193 else if(strncmp(str,NOTE,strlen(NOTE))==0) {
196 else if(strncmp(str,WARN,strlen(WARN))==0) {
198 sprintf(
string+len,
"%s",ANSI_CYAN) ;
199 len += strlen(
string+len) ;
201 else if(strncmp(str,CRIT,strlen(CRIT))==0) {
204 sprintf(
string+len,
"%s%s%s",ANSI_RED,ANSI_BOLD,ANSI_REVERSE) ;
205 len += strlen(
string+len) ;
207 else if(strncmp(str,ERR,strlen(ERR))==0) {
210 sprintf(
string+len,
"%s",ANSI_RED) ;
211 len += strlen(
string+len) ;
213 else if(strncmp(str,CAUTION,strlen(CAUTION))==0) {
216 sprintf(
string+len,
"%s%s",ANSI_MAGENTA,ANSI_REVERSE) ;
217 len += strlen(
string+len) ;
219 else if(strncmp(str,TERR,strlen(TERR))==0) {
222 sprintf(
string+len,
"%s",ANSI_GREEN) ;
223 len += strlen(
string+len) ;
225 else if(strncmp(str,OPER,strlen(OPER))==0) {
228 sprintf(
string+len,
"%s%s",ANSI_BLUE,ANSI_REVERSE) ;
229 len += strlen(
string+len) ;
231 else if(strncmp(str,
"U_",2)==0) {
234 sprintf(
string+len,
"%s",ANSI_REVERSE) ;
235 len += strlen(
string+len) ;
237 else if(strncmp(str,INFO,strlen(INFO))==0) {
240 sprintf(
string+len,
"%s",ANSI_BLUE) ;
241 len += strlen(
string+len) ;
252 sprintf(
string+len,
"(%s): ",cmd) ;
253 len += strlen(
string+len) ;
256 sprintf(
string+len,
"(%s): ",cmd_l) ;
257 len += strlen(
string+len) ;
264 vsprintf(
string+len,str,ap) ;
265 len += strlen(
string+len) ;
272 while(*(end-1) ==
'\n') {
278 sprintf(end,
"%s",ANSI_RESET) ;
282 strcat(
string+len,
"\n") ;
283 len += strlen(
string+len) ;
295 if((odesc >= 0) && (output_flag & RTS_LOG_NET)) {
296 ret = sendto(odesc,(
char *)buffer,len,0,
297 (
struct sockaddr *)&serverAddr,sockAddrSize) ;
301 syslog(LOG_USER|LOG_ERR,
"LOG sendto returned %d [%m]\n",ret) ;
312 if(output_flag & RTS_LOG_STDERR) {
313 fprintf(stderr,
"%s",(
char *)buffer+4) ;
317 if(fdesc && to_file && (output_flag & RTS_LOG_FILE)) {
319 struct tm *tm = localtime(&t) ;
321 if(fdesc) fprintf(fdesc,
"%02d:%02d:%02d: %s",tm->tm_hour,tm->tm_min,tm->tm_sec,(
char*)buffer+4) ;
326 struct tm *tm = localtime(&t) ;
328 FILE *f = fopen(jml_fname,
"a");
329 if(f) fprintf(f,
"%02d:%02d:%02d: %s",tm->tm_hour,tm->tm_min,tm->tm_sec,(
char*)buffer+4) ;
337 static const char *getCmd(
void)
341 static const char *str =
"(no-name)" ;
342 #if defined(__linux__) || defined(__APPLE__)
344 static char name[128] ;
347 file = fopen(
"/proc/self/cmdline",
"r") ;
348 if(file==NULL)
return str ;
350 fscanf(file,
"%120s",name) ;
353 if((ptr = strrchr(name,
'/'))) {
361 static struct psinfo ps ;
363 fd = open(
"/proc/self/psinfo",O_RDONLY,0666) ;
364 if(fd < 0)
return str ;
366 ret = read(fd,(
char *)&ps,
sizeof(ps)) ;
369 if(ret !=
sizeof(ps)) {