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 <stdarg.h>
30 : #include <stdlib.h>
31 : #include <unistd.h>
32 : #include <errno.h>
33 : #include <string.h>
34 : #include <sys/types.h>
35 : #include <strings.h>
36 : #include <ctype.h>
37 : #include "error.h"
38 :
39 : static int verbose_yn=0;
40 : static int debug_yn=0;
41 :
42 :
43 1 : void setverbose()
44 : {
45 1 : verbose_yn=1;
46 1 : }
47 3 : int getverbose()
48 : {
49 3 : return(verbose_yn);
50 : }
51 1 : void clrverbose()
52 : {
53 1 : verbose_yn=0;
54 1 : }
55 :
56 1 : void setdebug()
57 : {
58 1 : debug_yn=1;
59 1 : }
60 2 : int getdebug()
61 : {
62 2 : return(debug_yn);
63 : }
64 0 : void clrdebug()
65 : {
66 0 : debug_yn=0;
67 0 : }
68 :
69 6 : int error( char *message, ...)
70 : {
71 : va_list varg;
72 6 : va_start (varg, message);
73 6 : vfprintf(stderr, message,varg);
74 :
75 6 : va_end(varg);
76 6 : return 0;
77 : }
78 :
79 4 : int alert( char *message, ...)
80 : {
81 :
82 : va_list varg;
83 4 : va_start (varg, message);
84 :
85 4 : vfprintf(stderr, message,varg);
86 :
87 4 : va_end(varg);
88 :
89 4 : return 0;
90 : }
91 :
92 6 : int info( char *message, ...)
93 : {
94 6 : if (verbose_yn)
95 : {
96 : va_list varg;
97 1 : va_start (varg, message);
98 :
99 1 : vfprintf(stderr, message,varg);
100 :
101 1 : va_end(varg);
102 : }
103 6 : return 0;
104 : }
105 65 : int debug( char *message, ...)
106 : {
107 65 : if (debug_yn)
108 : {
109 : va_list varg;
110 1 : va_start (varg, message);
111 :
112 1 : vfprintf(stderr, message,varg);
113 :
114 1 : va_end(varg);
115 : }
116 65 : return 0;
117 : }
118 :
119 4 : int sock_error(char * msg)
120 : {
121 4 : int e=errno;
122 :
123 :
124 4 : fprintf(stderr,"%s, sock_err:#%d : %s\n", msg, e, strerror(e) );
125 :
126 4 : return e; // return error Num in case we need it later
127 : }
128 :
129 4 : void sock_error_no(char * msg,int e)
130 : {
131 :
132 4 : fprintf(stderr,"%s, sock_err:#%d : %s\n", msg, e, strerror(e) );
133 :
134 4 : return; // return error Num in case we need it later
135 : }
136 :
137 0 : int hex_dump_msg(const char*buf,ssize_t size_peek,const char *message, ...)
138 : {
139 : int i,j;
140 0 : char *datapeek=(char*) buf;
141 0 : char * li= calloc(size_peek, 6);
142 : va_list varg;
143 : // pthread_mutex_lock(&info_mutex);
144 0 : if (li)
145 : {
146 0 : va_start (varg, message);
147 0 : vsnprintf(li, 6*size_peek , message, varg);
148 :
149 : //sprintf(li,"%s \n",msg);
150 0 : for ( i=0;i<size_peek;i++)
151 : {
152 0 : if ((i%32)==0)
153 : {
154 0 : if (i) sprintf(li+strlen(li)," ");
155 0 : if (i) for (j=(i-32);(j!=i);j++)
156 : {
157 0 : if (isprint(datapeek[j]))
158 0 : sprintf(li+strlen(li),"%c",datapeek[j]);
159 : else
160 0 : sprintf(li+strlen(li),".");
161 : }
162 0 : if (i!=0) sprintf(li+strlen(li),"\n");
163 : }
164 0 : sprintf(li+strlen(li),"%02x ",(u_int8_t)datapeek[i]);
165 : }
166 0 : if ((i%32))
167 : {
168 0 : if (i) for (j=(i%32);(j!=32);j++) sprintf(li+strlen(li),".. ");
169 0 : if (i) for (j=(i-(i%32));(j!=i);j++)
170 : {
171 0 : if (isprint(datapeek[j]))
172 0 : sprintf(li+strlen(li),"%c",datapeek[j]);
173 : else
174 0 : sprintf(li+strlen(li),".");
175 : }
176 : //if (i!=0)sprintf(li+strlen(li),"\n");
177 : }
178 : //printf("%02x ",(u_int8_t)datapeek[i]);
179 0 : va_end(varg);
180 0 : if ((i%32)) sprintf(li+strlen(li),"\n");
181 :
182 : //sprintf(li+strlen(li),""); // TODO find other solution to add \0
183 :
184 0 : fprintf(stderr, "%s",li);
185 : //printf("%s",li);
186 :
187 0 : free(li);
188 : }
189 : //pthread_mutex_unlock(&info_mutex);
190 0 : return 1;
191 : }
|