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
, ...)
95 va_start(args
, format
);
96 cb(level
, format
, args
);
100 static const char hexdig
[] = "0123456789abcdef";
102 void RTMP_LogHex(int level
, const uint8_t *data
, unsigned long len
)
107 if ( level
> RTMP_debuglevel
)
112 for(i
=0; i
<len
; i
++) {
113 *ptr
++ = hexdig
[0x0f & (data
[i
] >> 4)];
114 *ptr
++ = hexdig
[0x0f & data
[i
]];
115 if ((i
& 0x0f) == 0x0f) {
118 RTMP_Log(level
, "%s", line
);
125 RTMP_Log(level
, "%s", line
);
129 void RTMP_LogHexString(int level
, const uint8_t *data
, unsigned long len
)
137 if ( !data
|| level
> RTMP_debuglevel
)
140 /* in case len is zero */
143 for ( i
= 0 ; i
< len
; i
++ ) {
148 if( i
) RTMP_Log( level
, "%s", line
);
149 memset( line
, ' ', sizeof(line
)-2 );
150 line
[sizeof(line
)-2] = '\0';
154 line
[2] = hexdig
[0x0f & (off
>> 12)];
155 line
[3] = hexdig
[0x0f & (off
>> 8)];
156 line
[4] = hexdig
[0x0f & (off
>> 4)];
157 line
[5] = hexdig
[0x0f & off
];
161 off
= BP_OFFSET
+ n
*3 + ((n
>= 8)?1:0);
162 line
[off
] = hexdig
[0x0f & ( data
[i
] >> 4 )];
163 line
[off
+1] = hexdig
[0x0f & data
[i
]];
165 off
= BP_GRAPH
+ n
+ ((n
>= 8)?1:0);
167 if ( isprint( data
[i
] )) {
168 line
[BP_GRAPH
+ n
] = data
[i
];
170 line
[BP_GRAPH
+ n
] = '.';
174 RTMP_Log( level
, "%s", line
);
177 /* These should only be used by apps, never by the library itself */
178 void RTMP_LogPrintf(const char *format
, ...)
180 char str
[MAX_PRINT_LEN
]="";
183 va_start(args
, format
);
184 len
= vsnprintf(str
, MAX_PRINT_LEN
-1, format
, args
);
187 if ( RTMP_debuglevel
==RTMP_LOGCRIT
)
190 if ( !fmsg
) fmsg
= stderr
;
197 if (len
> MAX_PRINT_LEN
-1)
198 len
= MAX_PRINT_LEN
-1;
199 fprintf(fmsg
, "%s", str
);
200 if (str
[len
-1] == '\n')
204 void RTMP_LogStatus(const char *format
, ...)
206 char str
[MAX_PRINT_LEN
]="";
208 va_start(args
, format
);
209 vsnprintf(str
, MAX_PRINT_LEN
-1, format
, args
);
212 if ( RTMP_debuglevel
==RTMP_LOGCRIT
)
215 if ( !fmsg
) fmsg
= stderr
;
217 fprintf(fmsg
, "%s", str
);