ppc_log_add.c

00001 /*
00002  * Copyright (c) 2005-2012 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.13 2007/11/09 22:06:26 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 <klone/server_ppc_cmd.h>
00019 #include "server_s.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     u_strlcpy(la.log, str, sizeof la.log);
00065 
00066     /* send the command request */
00067     nop_err_if(ppc_write(s->ppc, ctx->pipc, PPC_CMD_LOG_ADD, (char*)&la, 
00068         sizeof(la)) < 0);
00069 
00070     return 0;
00071 err:
00072     return ~0;
00073 }
00074 
00075 /* [parent] log a new message */
00076 int server_ppc_cb_log_add(ppc_t *ppc, int fd, unsigned char cmd, char *data, 
00077     size_t size, void *vso)
00078 {
00079     server_t *s;
00080     ppc_log_add_t *pla;
00081     backend_t *be;
00082     klog_t *kl;
00083 
00084     u_unused_args(ppc, fd, cmd, size);
00085 
00086     nop_err_if (vso == NULL);
00087     nop_err_if (data == NULL);
00088 
00089     pla = (ppc_log_add_t *) data;
00090     s = (server_t *) vso;
00091 
00092     /* by default use server logger */
00093     kl = s->klog;
00094 
00095     /* get the logger obj of the calling backend (if any) */
00096     if(!server_get_backend_by_id(s, pla->bid, &be) && be->klog)
00097         kl = be->klog;
00098 
00099     /* log the line */
00100     if(kl)
00101         nop_err_if(klog(kl, syslog_to_klog(pla->level), "%s", pla->log));
00102 
00103     return 0;
00104 err:
00105     return ~0;
00106 }
00107 

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