2 * Copyright (C) 2008-2009 Andrej Stepanchuk
3 * Copyright (C) 2009-2010 Howard Chu
5 * This file is part of librtmp.
7 * librtmp is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1,
10 * or (at your option) any later version.
12 * librtmp is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with librtmp see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/lgpl.html
33 #define MAX_PRINT_LEN 2048
35 RTMP_LogLevel RTMP_debuglevel
= RTMP_LOGERROR
;
41 static RTMP_LogCallback rtmp_log_default
, *cb
= rtmp_log_default
;
43 static const char *levels
[] = {
44 "CRIT", "ERROR", "WARNING", "INFO",
48 static void rtmp_log_default(int level
, const char *format
, va_list vl
)
50 char str
[MAX_PRINT_LEN
]="";
52 vsnprintf(str
, MAX_PRINT_LEN
-1, format
, vl
);
54 /* Filter out 'no-name' */
55 if ( RTMP_debuglevel
<RTMP_LOGALL
&& strstr(str
, "no-name" ) != NULL
)
58 if ( !fmsg
) fmsg
= stderr
;
60 if ( level
<= RTMP_debuglevel
) {
65 fprintf(fmsg
, "%s: %s\n", levels
[level
], str
);
72 void RTMP_LogSetOutput(FILE *file
)
77 void RTMP_LogSetLevel(RTMP_LogLevel level
)
79 RTMP_debuglevel
= level
;
82 void RTMP_LogSetCallback(RTMP_LogCallback
*cbp
)
87 RTMP_LogLevel
RTMP_LogGetLevel()
89 return RTMP_debuglevel
;
92 void RTMP_Log(int level
, const char *format
, ...)
96 if ( level
> RTMP_debuglevel
)
99 va_start(args
, format
);
100 cb(level
, format
, args
);
104 static const char hexdig
[] = "0123456789abcdef";
106 void RTMP_LogHex(int level
, const uint8_t *data
, unsigned long len
)
111 if ( level
> RTMP_debuglevel
)
116 for(i
=0; i
<len
; i
++) {
117 *ptr
++ = hexdig
[0x0f & (data
[i
] >> 4)];
118 *ptr
++ = hexdig
[0x0f & data
[i
]];
119 if ((i
& 0x0f) == 0x0f) {
122 RTMP_Log(level
, "%s", line
);
129 RTMP_Log(level
, "%s", line
);
133 void RTMP_LogHexString(int level
, const uint8_t *data
, unsigned long len
)
141 if ( !data
|| level
> RTMP_debuglevel
)
144 /* in case len is zero */
147 for ( i
= 0 ; i
< len
; i
++ ) {
152 if( i
) RTMP_Log( level
, "%s", line
);
153 memset( line
, ' ', sizeof(line
)-2 );
154 line
[sizeof(line
)-2] = '\0';
158 line
[2] = hexdig
[0x0f & (off
>> 12)];
159 line
[3] = hexdig
[0x0f & (off
>> 8)];
160 line
[4] = hexdig
[0x0f & (off
>> 4)];
161 line
[5] = hexdig
[0x0f & off
];
165 off
= BP_OFFSET
+ n
*3 + ((n
>= 8)?1:0);
166 line
[off
] = hexdig
[0x0f & ( data
[i
] >> 4 )];
167 line
[off
+1] = hexdig
[0x0f & data
[i
]];
169 off
= BP_GRAPH
+ n
+ ((n
>= 8)?1:0);
171 if ( isprint( data
[i
] )) {
172 line
[BP_GRAPH
+ n
] = data
[i
];
174 line
[BP_GRAPH
+ n
] = '.';
178 RTMP_Log( level
, "%s", line
);
181 /* These should only be used by apps, never by the library itself */
182 void RTMP_LogPrintf(const char *format
, ...)
184 char str
[MAX_PRINT_LEN
]="";
187 va_start(args
, format
);
188 len
= vsnprintf(str
, MAX_PRINT_LEN
-1, format
, args
);
191 if ( RTMP_debuglevel
==RTMP_LOGCRIT
)
194 if ( !fmsg
) fmsg
= stderr
;
201 if (len
> MAX_PRINT_LEN
-1)
202 len
= MAX_PRINT_LEN
-1;
203 fprintf(fmsg
, "%s", str
);
204 if (str
[len
-1] == '\n')
208 void RTMP_LogStatus(const char *format
, ...)
210 char str
[MAX_PRINT_LEN
]="";
212 va_start(args
, format
);
213 vsnprintf(str
, MAX_PRINT_LEN
-1, format
, args
);
216 if ( RTMP_debuglevel
==RTMP_LOGCRIT
)
219 if ( !fmsg
) fmsg
= stderr
;
221 fprintf(fmsg
, "%s", str
);