Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals

ppc_log_add.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: ppc_log_add.c,v 1.12 2006/01/10 16:16:59 tat Exp $
00009  */
00010 
00011 #include "klone_conf.h"
00012 #include <string.h>
00013 #include <klone/klog.h>
00014 #include <klone/context.h>
00015 #include <klone/server.h>
00016 #include <klone/ppc.h>
00017 #include <klone/ppc_cmd.h>
00018 #include "server_s.h"
00019 #include "server_ppc_cmd.h"
00020 
00021 /* struct used for ppc command PPC_CMD_LOG_ADD */
00022 struct ppc_log_add_s
00023 {
00024     int bid;                        /* calling backend ID       */
00025     int level;                      /* log level                */
00026     char log[U_MAX_LOG_LENGTH];     /* log line                 */
00027 };
00028 
00029 typedef struct ppc_log_add_s ppc_log_add_t;
00030 
00031 int syslog_to_klog(int level)
00032 {
00033     static int klog_lev[] = 
00034     { 
00035         KLOG_EMERG,
00036         KLOG_ALERT,
00037         KLOG_CRIT,
00038         KLOG_ERR,
00039         KLOG_WARNING,
00040         KLOG_NOTICE,
00041         KLOG_INFO,
00042         KLOG_DEBUG
00043     };
00044 
00045     if(level < LOG_EMERG || level > LOG_DEBUG)
00046         return KLOG_ALERT;
00047 
00048     return klog_lev[level];
00049 }
00050 
00051 /* client function */
00052 int server_ppc_cmd_log_add(server_t *s, int level, const char *str)
00053 {
00054     ppc_log_add_t la;
00055 
00056     nop_err_if (s == NULL);
00057     nop_err_if (s->ppc == NULL);
00058     nop_err_if (str == NULL);
00059 
00060     memset(&la, 0, sizeof(ppc_log_add_t));
00061 
00062     la.bid = ctx->backend->id;
00063     la.level = level;
00064     strncpy(la.log, str, U_MAX_LOG_LENGTH);
00065     la.log[U_MAX_LOG_LENGTH -1] = 0;
00066 
00067     /* send the command request */
00068     nop_err_if(ppc_write(s->ppc, ctx->pipc, PPC_CMD_LOG_ADD, (char*)&la, 
00069         sizeof(la)) < 0);
00070 
00071     return 0;
00072 err:
00073     return ~0;
00074 }
00075 
00076 /* [parent] log a new message */
00077 int server_ppc_cb_log_add(ppc_t *ppc, int fd, unsigned char cmd, char *data, 
00078     size_t size, void *vso)
00079 {
00080     server_t *s;
00081     ppc_log_add_t *pla;
00082     backend_t *be;
00083     klog_t *kl;
00084 
00085     u_unused_args(ppc, fd, cmd, size);
00086 
00087     nop_err_if (vso == NULL);
00088     nop_err_if (data == NULL);
00089 
00090     pla = (ppc_log_add_t *) data;
00091     s = (server_t *) vso;
00092 
00093     /* by default use server logger */
00094     kl = s->klog;
00095 
00096     /* get the logger obj of the calling backend (if any) */
00097     if(!server_get_backend_by_id(s, pla->bid, &be) && be->klog)
00098         kl = be->klog;
00099 
00100     /* log the line */
00101     if(kl)
00102         nop_err_if(klog(kl, syslog_to_klog(pla->level), "%s", pla->log));
00103 
00104     return 0;
00105 err:
00106     return ~0;
00107 }
00108 

←Products
© 2005-2006 - KoanLogic S.r.l. - All rights reserved