From 33c4e4c055a6e28d0c54d1047f0dfe85a8edb5b7 Mon Sep 17 00:00:00 2001 From: hyc Date: Sat, 10 Apr 2010 13:30:20 +0000 Subject: [PATCH] Restructure logging, add log callback git-svn-id: svn://svn.mplayerhq.hu/rtmpdump/trunk@422 400ebc74-4327-4243-bc38-086b20814532 --- librtmp/amf.c | 6 +- librtmp/handshake.h | 20 +++--- librtmp/log.c | 168 +++++++++++++++++++++++++------------------- librtmp/log.h | 3 + librtmp/rtmp.c | 10 +-- rtmpdump.c | 2 + 6 files changed, 118 insertions(+), 91 deletions(-) diff --git a/librtmp/amf.c b/librtmp/amf.c index ddb61ec..13663b4 100644 --- a/librtmp/amf.c +++ b/librtmp/amf.c @@ -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) { diff --git a/librtmp/handshake.h b/librtmp/handshake.h index 249a6fb..efebce0 100644 --- a/librtmp/handshake.h +++ b/librtmp/handshake.h @@ -137,7 +137,7 @@ GetDHOffset2(char *handshake, unsigned int len) 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, unsigned int len) 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 int len) 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, unsigned int len) 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); diff --git a/librtmp/log.c b/librtmp/log.c index b03f91d..b3a5455 100644 --- a/librtmp/log.c +++ b/librtmp/log.c @@ -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 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_debuglevel ) return; - if ( !fmsg ) fmsg = stderr; + ptr = line; - if ( level <= RTMP_debuglevel ) { - if (neednl) { - putc('\n', fmsg); - neednl = 0; + for(i=0; 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 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; } diff --git a/librtmp/log.h b/librtmp/log.h index e05b5ab..d2e50a0 100644 --- a/librtmp/log.h +++ b/librtmp/log.h @@ -24,6 +24,7 @@ #define __RTMP_LOG_H__ #include +#include #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, ...); diff --git a/librtmp/rtmp.c b/librtmp/rtmp.c index 13bfc7c..9d844a8 100644 --- a/librtmp/rtmp.c +++ b/librtmp/rtmp.c @@ -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, unsigned int len) { AMFObjectProperty prop; // Show metadata - RTMP_LogPrintf("Metadata:\n"); + RTMP_Log(RTMP_LOGINFO, "Metadata:"); DumpMetaData(&obj); if (RTMP_FindFirstMatchingProperty(&obj, &av_duration, &prop)) { diff --git a/rtmpdump.c b/rtmpdump.c index a659afc..d230b4e 100644 --- a/rtmpdump.c +++ b/rtmpdump.c @@ -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) -- 2.50.1