1 /*
2  * Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org>
3  * Copyright (c) 2006-2015 Sippy Software, Inc., http://www.sippysoft.com
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <stdarg.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 
34 #include "rtpp_types.h"
35 #include "rtpp_log.h"
36 #include "rtpp_log_obj.h"
37 #include "rtpp_log_obj_fin.h"
38 #include "rtpp_genuid_singlet.h"
39 #include "rtpp_mallocs.h"
40 #include "rtpp_refcnt.h"
41 
42 struct rtpp_log_priv
43 {
44     struct rtpp_log pub;
45     rtpp_log_t log;
46 };
47 
48 #define PUB2PVT(pubp) \
49   ((struct rtpp_log_priv *)((char *)(pubp) - offsetof(struct rtpp_log_priv, pub)))
50 
51 static void rtpp_log_obj_dtor(struct rtpp_log_priv *);
52 static void rtpp_log_obj_setlevel(struct rtpp_log *, int);
53 static void rtpp_log_obj_write(struct rtpp_log *, const char *, int, const char *, ...);
54 static void rtpp_log_obj_ewrite(struct rtpp_log *, const char *, int, const char *, ...);
55 
56 struct rtpp_log *
rtpp_log_ctor(struct rtpp_cfg_stable * cfs,const char * app,const char * call_id,int flags)57 rtpp_log_ctor(struct rtpp_cfg_stable *cfs, const char *app,
58   const char *call_id, int flags)
59 {
60     struct rtpp_log_priv *pvt;
61     struct rtpp_refcnt *rcnt;
62 
63     pvt = rtpp_rzmalloc(sizeof(struct rtpp_log_priv), &rcnt);
64     if (pvt == NULL) {
65         return (NULL);
66     }
67     pvt->pub.rcnt = rcnt;
68     pvt->log = rtpp_log_open(cfs, app, call_id, flags);
69     rtpp_gen_uid(&pvt->pub.lguid);
70     pvt->pub.setlevel = &rtpp_log_obj_setlevel;
71     pvt->pub.write = rtpp_log_obj_write;
72     pvt->pub.ewrite = rtpp_log_obj_ewrite;
73     CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_log_obj_dtor,
74       pvt);
75     return (&pvt->pub);
76 }
77 
78 static void
rtpp_log_obj_dtor(struct rtpp_log_priv * pvt)79 rtpp_log_obj_dtor(struct rtpp_log_priv *pvt)
80 {
81 
82     rtpp_log_fin(&pvt->pub);
83     rtpp_log_close(pvt->log);
84     free(pvt);
85 }
86 
87 static void
rtpp_log_obj_setlevel(struct rtpp_log * self,int log_level)88 rtpp_log_obj_setlevel(struct rtpp_log *self, int log_level)
89 {
90     struct rtpp_log_priv *pvt;
91 
92     pvt = PUB2PVT(self);
93     if (log_level != -1) {
94         rtpp_log_setlevel(pvt->log, log_level);
95     } else {
96         rtpp_log_setlevel(pvt->log, RTPP_LOG_ERR);
97     }
98 }
99 
100 static void
rtpp_log_obj_write(struct rtpp_log * self,const char * fname,int level,const char * fmt,...)101 rtpp_log_obj_write(struct rtpp_log *self, const char *fname, int level,
102   const char *fmt, ...)
103 {
104     va_list ap;
105     struct rtpp_log_priv *pvt;
106 
107     pvt = PUB2PVT(self);
108     va_start(ap, fmt);
109     _rtpp_log_write_va(pvt->log, level, fname, fmt, ap);
110     va_end(ap);
111     return;
112 }
113 
114 static void
rtpp_log_obj_ewrite(struct rtpp_log * self,const char * fname,int level,const char * fmt,...)115 rtpp_log_obj_ewrite(struct rtpp_log *self, const char *fname, int level,
116   const char *fmt, ...)
117 {
118     va_list ap;
119     struct rtpp_log_priv *pvt;
120 
121     pvt = PUB2PVT(self);
122     va_start(ap, fmt);
123     _rtpp_log_ewrite_va(pvt->log, level, fname, fmt, ap);
124     va_end(ap);
125     return;
126 }
127