r422 - in trunk: librtmp/amf.c librtmp/handshake.h librtmp/log.c librtmp/log.h librtmp/rtmp.c rtmpdump.c
Author: hyc Date: Sat Apr 10 15:30:20 2010 New Revision: 422 Log: Restructure logging, add log callback Modified: trunk/librtmp/amf.c trunk/librtmp/handshake.h trunk/librtmp/log.c trunk/librtmp/log.h trunk/librtmp/rtmp.c trunk/rtmpdump.c Modified: trunk/librtmp/amf.c ============================================================================== --- trunk/librtmp/amf.c Wed Apr 7 09:34:37 2010 (r421) +++ trunk/librtmp/amf.c Sat Apr 10 15:30:20 2010 (r422) @@ -804,10 +804,10 @@ AMFProp_Dump(AMFObjectProperty *prop) name.av_val = "no-name."; name.av_len = sizeof("no-name.") - 1; } - if (name.av_len > 25) - name.av_len = 25; + if (name.av_len > 18) + name.av_len = 18; - snprintf(strRes, 255, "Name: %25.*s, ", name.av_len, name.av_val); + snprintf(strRes, 255, "Name: %18.*s, ", name.av_len, name.av_val); if (prop->p_type == AMF_OBJECT) { Modified: trunk/librtmp/handshake.h ============================================================================== --- trunk/librtmp/handshake.h Wed Apr 7 09:34:37 2010 (r421) +++ trunk/librtmp/handshake.h Sat Apr 10 15:30:20 2010 (r422) @@ -137,7 +137,7 @@ GetDHOffset2(char *handshake, unsigned i if (res + 128 > 767) { RTMP_Log(RTMP_LOGERROR, - "%s: Couldn't calculate correct DH offset (got %d), exiting!\n", + "%s: Couldn't calculate correct DH offset (got %d), exiting!", __FUNCTION__, res); exit(1); } @@ -163,7 +163,7 @@ GetDigestOffset2(char *handshake, unsign if (res + 32 > 1535) { RTMP_Log(RTMP_LOGERROR, - "%s: Couldn't calculate correct digest offset (got %d), exiting\n", + "%s: Couldn't calculate correct digest offset (got %d), exiting", __FUNCTION__, res); exit(1); } @@ -190,7 +190,7 @@ GetDHOffset1(char *handshake, unsigned i if (res + 128 > 1531) { - RTMP_Log(RTMP_LOGERROR, "%s: Couldn't calculate DH offset (got %d), exiting!\n", + RTMP_Log(RTMP_LOGERROR, "%s: Couldn't calculate DH offset (got %d), exiting!", __FUNCTION__, res); exit(1); } @@ -218,8 +218,8 @@ GetDigestOffset1(char *handshake, unsign if (res + 32 > 771) { - RTMP_Log(RTMP_LOGDEBUG, - "%s: Couldn't calculate digest offset (got %d), exiting!\n", + RTMP_Log(RTMP_LOGERROR, + "%s: Couldn't calculate digest offset (got %d), exiting!", __FUNCTION__, res); exit(1); } @@ -380,7 +380,7 @@ HandShake(RTMP * r, bool FP9HandShake) clientsig[5] = 0; clientsig[7] = 2; - RTMP_Log(RTMP_LOGDEBUG, "%s: Client type: %02X\n", __FUNCTION__, clientsig[-1]); + RTMP_Log(RTMP_LOGDEBUG, "%s: Client type: %02X", __FUNCTION__, clientsig[-1]); } else { @@ -485,12 +485,12 @@ HandShake(RTMP * r, bool FP9HandShake) if (!VerifyDigest(digestPosServer, serversig, GenuineFMSKey, 36)) { - RTMP_Log(RTMP_LOGWARNING, "Trying different position for server digest!\n"); + RTMP_Log(RTMP_LOGWARNING, "Trying different position for server digest!"); digestPosServer = GetDigestOffset1(serversig, RTMP_SIG_SIZE); if (!VerifyDigest(digestPosServer, serversig, GenuineFMSKey, 36)) { - RTMP_Log(RTMP_LOGERROR, "Couldn't verify the server digest\n"); /* continuing anyway will probably fail */ + RTMP_Log(RTMP_LOGERROR, "Couldn't verify the server digest"); /* continuing anyway will probably fail */ return false; } dhposServer = GetDHOffset1(serversig, RTMP_SIG_SIZE); @@ -847,12 +847,12 @@ SHandShake(RTMP * r) if (!VerifyDigest(digestPosClient, clientsig, GenuineFPKey, 30)) { - RTMP_Log(RTMP_LOGWARNING, "Trying different position for client digest!\n"); + RTMP_Log(RTMP_LOGWARNING, "Trying different position for client digest!"); digestPosClient = GetDigestOffset2(clientsig, RTMP_SIG_SIZE); if (!VerifyDigest(digestPosClient, clientsig, GenuineFPKey, 30)) { - RTMP_Log(RTMP_LOGERROR, "Couldn't verify the client digest\n"); /* continuing anyway will probably fail */ + RTMP_Log(RTMP_LOGERROR, "Couldn't verify the client digest"); /* continuing anyway will probably fail */ return false; } dhposClient = GetDHOffset2(clientsig, RTMP_SIG_SIZE); Modified: trunk/librtmp/log.c ============================================================================== --- trunk/librtmp/log.c Wed Apr 7 09:34:37 2010 (r421) +++ trunk/librtmp/log.c Sat Apr 10 15:30:20 2010 (r422) @@ -36,11 +36,37 @@ static int neednl; static FILE *fmsg; +static RTMP_LogCallback rtmp_log, *cb = rtmp_log; + static const char *levels[] = { "CRIT", "ERROR", "WARNING", "INFO", "DEBUG", "DEBUG2" }; +static void rtmp_log(int level, const char *format, va_list vl) +{ + char str[MAX_PRINT_LEN]=""; + + vsnprintf(str, MAX_PRINT_LEN-1, format, vl); + + // Filter out 'no-name' + if ( RTMP_debuglevel<RTMP_LOGALL && strstr(str, "no-name" ) != NULL ) + return; + + if ( !fmsg ) fmsg = stderr; + + if ( level <= RTMP_debuglevel ) { + if (neednl) { + putc('\n', fmsg); + neednl = 0; + } + fprintf(fmsg, "%s: %s\n", levels[level], str); +#ifdef _DEBUG + fflush(fmsg); +#endif + } +} + void RTMP_LogSetOutput(FILE *file) { fmsg = file; @@ -56,92 +82,45 @@ RTMP_LogLevel RTMP_LogGetLevel() return RTMP_debuglevel; } -void RTMP_LogPrintf(const char *format, ...) +void RTMP_Log(int level, const char *format, ...) { - char str[MAX_PRINT_LEN]=""; - int len; va_list args; va_start(args, format); - len = vsnprintf(str, MAX_PRINT_LEN-1, format, args); + cb(level, format, args); va_end(args); - - if ( RTMP_debuglevel==RTMP_LOGCRIT ) - return; - - if ( !fmsg ) fmsg = stderr; - - if (neednl) { - putc('\n', fmsg); - neednl = 0; - } - - if (len > MAX_PRINT_LEN-1) - len = MAX_PRINT_LEN-1; - fprintf(fmsg, "%s", str); - if (str[len-1] == '\n') - fflush(fmsg); } -void RTMP_LogStatus(const char *format, ...) -{ - char str[MAX_PRINT_LEN]=""; - va_list args; - va_start(args, format); - vsnprintf(str, MAX_PRINT_LEN-1, format, args); - va_end(args); - - if ( RTMP_debuglevel==RTMP_LOGCRIT ) - return; - - if ( !fmsg ) fmsg = stderr; - - fprintf(fmsg, "%s", str); - fflush(fmsg); - neednl = 1; -} +static const char hexdig[] = "0123456789abcdef"; -void RTMP_Log(int level, const char *format, ...) +void RTMP_LogHex(int level, const char *data, unsigned long len) { - char str[MAX_PRINT_LEN]=""; - va_list args; - va_start(args, format); - vsnprintf(str, MAX_PRINT_LEN-1, format, args); - va_end(args); + unsigned long i; + char line[50], *ptr; - // Filter out 'no-name' - if ( RTMP_debuglevel<RTMP_LOGALL && strstr(str, "no-name" ) != NULL ) + if ( level > RTMP_debuglevel ) return; - if ( !fmsg ) fmsg = stderr; + ptr = line; - if ( level <= RTMP_debuglevel ) { - if (neednl) { - putc('\n', fmsg); - neednl = 0; + for(i=0; i<len; i++) { + *ptr++ = hexdig[0x0f & (data[i] >> 4)]; + *ptr++ = hexdig[0x0f & data[i]]; + if ((i & 0x0f) == 0x0f) { + *ptr = '\0'; + ptr = line; + RTMP_Log(level, "%s", line); + } else { + *ptr++ = ' '; } - fprintf(fmsg, "%s: %s\n", levels[level], str); -#ifdef _DEBUG - fflush(fmsg); -#endif } -} - -void RTMP_LogHex(int level, const char *data, unsigned long len) -{ - unsigned long i; - if ( level > RTMP_debuglevel ) - return; - for(i=0; i<len; i++) { - RTMP_LogPrintf("%02X%c", (unsigned char)data[i], - ((i & 0x0f) == 0x0f) ? '\n': ' '); + if (i & 0x0f) { + *ptr = '\0'; + RTMP_Log(level, "%s", line); } - if (i & 0x0f) - RTMP_LogPrintf("\n"); } void RTMP_LogHexString(int level, const char *data, unsigned long len) { - static const char hexdig[] = "0123456789abcdef"; #define BP_OFFSET 9 #define BP_GRAPH 60 #define BP_LEN 80 @@ -152,18 +131,16 @@ void RTMP_LogHexString(int level, const return; /* in case len is zero */ - line[0] = '\n'; - line[1] = '\0'; + line[0] = '\0'; for ( i = 0 ; i < len ; i++ ) { int n = i % 16; unsigned off; if( !n ) { - if( i ) RTMP_LogPrintf( "%s", line ); + if( i ) RTMP_Log( level, "%s", line ); memset( line, ' ', sizeof(line)-2 ); - line[sizeof(line)-2] = '\n'; - line[sizeof(line)-1] = '\0'; + line[sizeof(line)-2] = '\0'; off = i % 0x0ffffU; @@ -187,5 +164,50 @@ void RTMP_LogHexString(int level, const } } - RTMP_LogPrintf( "%s", line ); + RTMP_Log( level, "%s", line ); +} + +/* These should only be used by apps, never by the library itself */ +void RTMP_LogPrintf(const char *format, ...) +{ + char str[MAX_PRINT_LEN]=""; + int len; + va_list args; + va_start(args, format); + len = vsnprintf(str, MAX_PRINT_LEN-1, format, args); + va_end(args); + + if ( RTMP_debuglevel==RTMP_LOGCRIT ) + return; + + if ( !fmsg ) fmsg = stderr; + + if (neednl) { + putc('\n', fmsg); + neednl = 0; + } + + if (len > MAX_PRINT_LEN-1) + len = MAX_PRINT_LEN-1; + fprintf(fmsg, "%s", str); + if (str[len-1] == '\n') + fflush(fmsg); +} + +void RTMP_LogStatus(const char *format, ...) +{ + char str[MAX_PRINT_LEN]=""; + va_list args; + va_start(args, format); + vsnprintf(str, MAX_PRINT_LEN-1, format, args); + va_end(args); + + if ( RTMP_debuglevel==RTMP_LOGCRIT ) + return; + + if ( !fmsg ) fmsg = stderr; + + fprintf(fmsg, "%s", str); + fflush(fmsg); + neednl = 1; } Modified: trunk/librtmp/log.h ============================================================================== --- trunk/librtmp/log.h Wed Apr 7 09:34:37 2010 (r421) +++ trunk/librtmp/log.h Sat Apr 10 15:30:20 2010 (r422) @@ -24,6 +24,7 @@ #define __RTMP_LOG_H__ #include <stdio.h> +#include <stdarg.h> #ifdef __cplusplus extern "C" { @@ -42,6 +43,8 @@ typedef enum extern RTMP_LogLevel RTMP_debuglevel; +typedef void (RTMP_LogCallback)(int level, const char *fmt, va_list); +void RTMP_LogSetCallback(RTMP_LogCallback *cb); void RTMP_LogSetOutput(FILE *file); void RTMP_LogPrintf(const char *format, ...); void RTMP_LogStatus(const char *format, ...); Modified: trunk/librtmp/rtmp.c ============================================================================== --- trunk/librtmp/rtmp.c Wed Apr 7 09:34:37 2010 (r421) +++ trunk/librtmp/rtmp.c Sat Apr 10 15:30:20 2010 (r422) @@ -466,9 +466,9 @@ static void RTMP_OptUsage() { int i; - RTMP_LogPrintf("Valid RTMP options are:\n"); + RTMP_Log(RTMP_LOGERROR, "Valid RTMP options are:\n"); for (i=0; options[i].name.av_len; i++) { - RTMP_LogPrintf("%10s %-7s %s\n", options[i].name.av_val, + RTMP_Log(RTMP_LOGERROR, "%10s %-7s %s\n", options[i].name.av_val, optinfo[options[i].otype], options[i].use); } } @@ -2364,14 +2364,14 @@ DumpMetaData(AMFObject *obj) // chomp if (strlen(str) >= 1 && str[strlen(str) - 1] == '\n') str[strlen(str) - 1] = '\0'; - RTMP_LogPrintf(" %-22.*s%s\n", prop->p_name.av_len, + RTMP_Log(RTMP_LOGINFO, " %-22.*s%s", prop->p_name.av_len, prop->p_name.av_val, str); } } else { if (prop->p_name.av_len) - RTMP_LogPrintf("%.*s:\n", prop->p_name.av_len, prop->p_name.av_val); + RTMP_Log(RTMP_LOGINFO, "%.*s:", prop->p_name.av_len, prop->p_name.av_val); DumpMetaData(&prop->p_vu.p_object); } } @@ -2405,7 +2405,7 @@ HandleMetadata(RTMP *r, char *body, unsi { AMFObjectProperty prop; // Show metadata - RTMP_LogPrintf("Metadata:\n"); + RTMP_Log(RTMP_LOGINFO, "Metadata:"); DumpMetaData(&obj); if (RTMP_FindFirstMatchingProperty(&obj, &av_duration, &prop)) { Modified: trunk/rtmpdump.c ============================================================================== --- trunk/rtmpdump.c Wed Apr 7 09:34:37 2010 (r421) +++ trunk/rtmpdump.c Sat Apr 10 15:30:20 2010 (r422) @@ -770,6 +770,8 @@ main(int argc, char **argv) signal(SIGQUIT, sigIntHandler); #endif + RTMP_debuglevel = RTMP_LOGINFO; + // Check for --quiet option before printing any output int index = 0; while (index < argc)
participants (1)
-
hyc