Line data Source code
1 : /*
2 : cJSON_deploy.h
3 : Created by Danny Goossen, Gioxa Ltd on 7/2/17.
4 :
5 : MIT License
6 :
7 : Copyright (c) 2017 deployctl, Gioxa Ltd.
8 :
9 : Permission is hereby granted, free of charge, to any person obtaining a copy
10 : of this software and associated documentation files (the "Software"), to deal
11 : in the Software without restriction, including without limitation the rights
12 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 : copies of the Software, and to permit persons to whom the Software is
14 : furnished to do so, subject to the following conditions:
15 :
16 : The above copyright notice and this permission notice shall be included in all
17 : copies or substantial portions of the Software.
18 :
19 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 : SOFTWARE.
26 :
27 : */
28 : // LOGGING or OUTPUT with DEBUG/ALERT/INFO/ERROR/ SOCKETERROR
29 : #include <syslog.h>
30 : #include <stdarg.h>
31 : #include <stdlib.h>
32 : #include <unistd.h>
33 : #include <errno.h>
34 : #include <string.h>
35 : #include <sys/types.h>
36 : #include <strings.h>
37 : #include <ctype.h>
38 : #include "error.h"
39 :
40 : static int syslog_yn=0;
41 : static int verbose_yn=0;
42 : static int debug_yn=0;
43 :
44 :
45 3 : void setsyslog()
46 : {
47 3 : syslog_yn=1;
48 3 : }
49 2 : int getsyslog()
50 : {
51 2 : int r=syslog_yn;
52 2 : return(r);
53 : }
54 3 : void clrsyslog()
55 : {
56 3 : syslog_yn=0;
57 3 : }
58 :
59 2 : void setverbose()
60 : {
61 2 : verbose_yn=1;
62 2 : }
63 3 : int getverbose()
64 : {
65 3 : return(verbose_yn);
66 : }
67 1 : void clrverbose()
68 : {
69 1 : verbose_yn=0;
70 1 : }
71 :
72 3 : void setdebug()
73 : {
74 3 : debug_yn=1;
75 3 : }
76 3 : int getdebug()
77 : {
78 3 : return(debug_yn);
79 : }
80 1 : void clrdebug()
81 : {
82 1 : debug_yn=0;
83 1 : }
84 :
85 16 : int error( char *message, ...)
86 : {
87 : va_list varg;
88 16 : va_start (varg, message);
89 16 : if (syslog_yn)
90 3 : vsyslog (LOG_ERR,message,varg);
91 : else
92 13 : vfprintf(stderr, message,varg);
93 :
94 16 : va_end(varg);
95 16 : return 0;
96 : }
97 :
98 12 : int alert( char *message, ...)
99 : {
100 :
101 : va_list varg;
102 12 : va_start (varg, message);
103 :
104 12 : if (syslog_yn)
105 3 : vsyslog (LOG_ALERT,message,varg);
106 : else
107 9 : vfprintf(stderr, message,varg);
108 :
109 12 : va_end(varg);
110 :
111 12 : return 0;
112 : }
113 :
114 6 : int info( char *message, ...)
115 : {
116 6 : if (verbose_yn)
117 : {
118 : va_list varg;
119 2 : va_start (varg, message);
120 2 : if (syslog_yn)
121 1 : vsyslog (LOG_NOTICE,message,varg);
122 : else
123 1 : vfprintf(stdout, message,varg);
124 :
125 2 : va_end(varg);
126 : }
127 6 : return 0;
128 : }
129 970 : int debug( char *message, ...)
130 : {
131 970 : if (debug_yn)
132 : {
133 : va_list varg;
134 48 : va_start (varg, message);
135 48 : if (syslog_yn)
136 1 : vsyslog (LOG_NOTICE,message,varg);
137 : else
138 47 : vfprintf(stdout, message,varg);
139 :
140 48 : va_end(varg);
141 : }
142 970 : return 0;
143 : }
144 :
145 32 : int sock_error(char * msg)
146 : {
147 32 : int e=errno;
148 :
149 32 : if (syslog_yn)
150 3 : syslog( LOG_ERR,"%s, sock_err:#%d : %s", msg, e, strerror(e) );
151 : else
152 29 : fprintf(stderr,"%s, sock_err:#%d : %s\n", msg, e, strerror(e) );
153 :
154 32 : return e; // return error Num in case we need it later
155 : }
156 :
157 6 : void sock_error_no(char * msg,int e)
158 : {
159 6 : if (syslog_yn)
160 3 : syslog( LOG_ERR,"%s, sock_err:#%d : %s", msg, e, strerror(e) );
161 : else
162 3 : fprintf(stderr,"%s, sock_err:#%d : %s\n", msg, e, strerror(e) );
163 :
164 6 : return; // return error Num in case we need it later
165 : }
166 :
167 0 : int hex_dump_msg(const char*buf,ssize_t size_peek,const char *message, ...)
168 : {
169 : int i,j;
170 0 : char *datapeek=(char*) buf;
171 0 : char * li= calloc(size_peek, 6);
172 : va_list varg;
173 : // pthread_mutex_lock(&info_mutex);
174 0 : if (li)
175 : {
176 0 : va_start (varg, message);
177 0 : vsnprintf(li, 6*size_peek , message, varg);
178 :
179 : //sprintf(li,"%s \n",msg);
180 0 : for ( i=0;i<size_peek;i++)
181 : {
182 0 : if ((i%32)==0)
183 : {
184 0 : if (i) sprintf(li+strlen(li)," ");
185 0 : if (i) for (j=(i-32);(j!=i);j++)
186 : {
187 0 : if (isprint(datapeek[j]))
188 0 : sprintf(li+strlen(li),"%c",datapeek[j]);
189 : else
190 0 : sprintf(li+strlen(li),".");
191 : }
192 0 : if (i!=0) sprintf(li+strlen(li),"\n");
193 : }
194 0 : sprintf(li+strlen(li),"%02x ",(u_int8_t)datapeek[i]);
195 : }
196 0 : if ((i%32))
197 : {
198 0 : if (i) for (j=(i%32);(j!=32);j++) sprintf(li+strlen(li),".. ");
199 0 : if (i) for (j=(i-(i%32));(j!=i);j++)
200 : {
201 0 : if (isprint(datapeek[j]))
202 0 : sprintf(li+strlen(li),"%c",datapeek[j]);
203 : else
204 0 : sprintf(li+strlen(li),".");
205 : }
206 : //if (i!=0)sprintf(li+strlen(li),"\n");
207 : }
208 : //printf("%02x ",(u_int8_t)datapeek[i]);
209 0 : va_end(varg);
210 0 : if ((i%32)) sprintf(li+strlen(li),"\n");
211 :
212 : //sprintf(li+strlen(li),""); // TODO find other solution to add \0
213 :
214 0 : fprintf(stderr, "%s",li);
215 : //printf("%s",li);
216 :
217 0 : free(li);
218 : }
219 : //pthread_mutex_unlock(&info_mutex);
220 0 : return 1;
221 : }
|