ppc_log_get.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_get.c,v 1.4 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_GET */
00022 struct ppc_log_get_s
00023 {
00024     int bid;                        /* calling backend ID       */
00025     ssize_t i;                      /* i-th line                */
00026     char line[KLOG_LN_SZ];          /* log line                 */
00027 };
00028 
00029 typedef struct ppc_log_get_s ppc_log_get_t;
00030 
00031 /* client function */
00032 int server_ppc_cmd_log_get(server_t *s, size_t i, char *line)
00033 {
00034     ppc_log_get_t plg;
00035     unsigned char cmd;
00036 
00037     nop_err_if (s == NULL);
00038     nop_err_if (s->ppc == NULL);
00039     nop_err_if (ctx->backend == NULL);
00040     nop_err_if (line == NULL);
00041 
00042     memset(&plg, 0, sizeof(ppc_log_get_t));
00043 
00044     plg.bid = ctx->backend->id;
00045     plg.i = i;
00046     plg.line[0] = 0;
00047 
00048     /* send the command request */
00049     dbg_err_if(ppc_write(s->ppc, ctx->pipc, PPC_CMD_LOG_GET, (char*)&plg, 
00050         sizeof(plg)) < 0);
00051 
00052     /* get the response */
00053     dbg_err_if(ppc_read(s->ppc, ctx->pipc, &cmd, (char*)&plg, sizeof(plg)) < 0);
00054 
00055     dbg_err_if(cmd != PPC_CMD_LOG_GET);
00056 
00057     nop_err_if(plg.i < 0); /* error or eof */
00058 
00059     /* copy-out the line */
00060     u_strlcpy(line, plg.line, sizeof line);
00061 
00062     return 0;
00063 err:
00064     return ~0;
00065 }
00066 
00067 /* [parent] get a log line */
00068 int server_ppc_cb_log_get(ppc_t *ppc, int fd, unsigned char cmd, char *data, 
00069     size_t size, void *vso)
00070 {
00071     server_t *s;
00072     ppc_log_get_t *plg;
00073     backend_t *be;
00074     klog_t *kl;
00075 
00076     u_unused_args(ppc, fd, cmd, size);
00077 
00078     dbg_err_if (vso == NULL);
00079     dbg_err_if (data == NULL);
00080 
00081     plg = (ppc_log_get_t *) data;
00082     s = (server_t *) vso;
00083 
00084     /* by default use server logger */
00085     kl = s->klog;
00086 
00087     /* get the logger obj of the calling backend (if any) */
00088     if(!server_get_backend_by_id(s, plg->bid, &be) && be->klog)
00089         kl = be->klog;
00090 
00091     /* get the log line */
00092     if(kl == NULL || klog_getln(kl, plg->i, plg->line))
00093         plg->i = -1; /* eof or error */
00094 
00095     /* send back the response */
00096     nop_err_if(ppc_write(s->ppc, fd, PPC_CMD_LOG_GET, (char*)plg, 
00097         sizeof(ppc_log_get_t)) < 0);
00098 
00099     return 0;
00100 err:
00101     return ~0;
00102 }
00103 

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