
- /**
- * Send the specified message to the log if the level is less than or equal
- * to the current av_log_level. By default, all logging messages are sent to
- * stderr. This behavior can be altered by setting a different logging callback
- * function.
- * @see av_log_set_callback
- *
- * @param avcl A pointer to an arbitrary struct of which the first field is a
- * pointer to an AVClass struct or NULL if general log.
- * @param level The importance level of the message expressed using a @ref
- * lavu_log_constants "Logging Constant".
- * @param fmt The format string (printf-compatible) that specifies how
- * subsequent arguments are converted to output.
- */
- void av_log(void *avcl, int level, const char *fmt, ...) av_printf_format(3, 4);
- /**
- * Print no output.
- */
- #define AV_LOG_QUIET -8
-
- /**
- * Something went really wrong and we will crash now.
- */
- #define AV_LOG_PANIC 0
-
- /**
- * Something went wrong and recovery is not possible.
- * For example, no header was found for a format which depends
- * on headers or an illegal combination of parameters is used.
- */
- #define AV_LOG_FATAL 8
-
- /**
- * Something went wrong and cannot losslessly be recovered.
- * However, not all future data is affected.
- */
- #define AV_LOG_ERROR 16
-
- /**
- * Something somehow does not look correct. This may or may not
- * lead to problems. An example would be the use of '-vstrict -2'.
- */
- #define AV_LOG_WARNING 24
-
- /**
- * Standard information.
- */
- #define AV_LOG_INFO 32
-
- /**
- * Detailed information.
- */
- #define AV_LOG_VERBOSE 40
-
- /**
- * Stuff which is only useful for libav* developers.
- */
- #define AV_LOG_DEBUG 48
-
- /**
- * Extremely verbose debugging, useful for libav* development.
- */
- #define AV_LOG_TRACE 56
-
- #define AV_LOG_MAX_OFFSET (AV_LOG_TRACE - AV_LOG_QUIET)
av_log_get_level()的定义如下所示
- /**
- * Get the current log level
- *
- * @see lavu_log_constants
- *
- * @return Current log level
- */
- int av_log_get_level(void);
- int av_log_get_level(void)
- {
- return av_log_level;
- }
- /**
- * Set the log level
- *
- * @see lavu_log_constants
- *
- * @param level Logging level
- */
- void av_log_set_level(int level);
- void av_log_set_level(int level)
- {
- av_log_level = level;
- }
- void av_log(void* avcl, int level, const char *fmt, ...)
- {
- va_list vl;
- va_start(vl, fmt);
- av_vlog(avcl, level, fmt, vl);
- va_end(vl);
- }
- #include <stdio.h>
- #include<stdarg.h>
- void fun(int a,...){
- va_list pp;
- va_start(pp,a);
- do{
- printf("param =%d\n",a);
- a=va_arg(pp,int);//使 pp 指向下一个参数,将下一个参数的值赋给变量 a
- }
- while (a!=0);//直到参数为 0 时停止循环
- }
-
- int main(){
- fun(20,40,60,80,0);
- return 0;
- }

- /**
- * Send the specified message to the log if the level is less than or equal
- * to the current av_log_level. By default, all logging messages are sent to
- * stderr. This behavior can be altered by setting a different logging callback
- * function.
- * @see av_log_set_callback
- *
- * @param avcl A pointer to an arbitrary struct of which the first field is a
- * pointer to an AVClass struct.
- * @param level The importance level of the message expressed using a @ref
- * lavu_log_constants "Logging Constant".
- * @param fmt The format string (printf-compatible) that specifies how
- * subsequent arguments are converted to output.
- * @param vl The arguments referenced by the format string.
- */
- void av_vlog(void *avcl, int level, const char *fmt, va_list vl);
- void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
- {
- AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
- void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
- if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
- avc->log_level_offset_offset && level >= AV_LOG_FATAL)
- level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
- if (log_callback)
- log_callback(avcl, level, fmt, vl);
- }
- static void (*av_log_callback)(void*, int, const char*, va_list) =
- av_log_default_callback;
- /**
- * Set the logging callback
- *
- * @note The callback must be thread safe, even if the application does not use
- * threads itself as some codecs are multithreaded.
- *
- * @see av_log_default_callback
- *
- * @param callback A logging function with a compatible signature.
- */
- void av_log_set_callback(void (*callback)(void*, int, const char*, va_list));
- void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
- {
- av_log_callback = callback;
- }
- void my_logoutput(void* ptr, int level, const char* fmt,va_list vl){
- FILE *fp = fopen("my_log.txt","a+");
- if(fp){
- vfprintf(fp,fmt,vl);
- fflush(fp);
- fclose(fp);
- }
- }
av_log_set_callback(my_logoutput);
下面我们看一下FFmpeg的默认Log输出函数av_log_default_callback()。它的定义如下。
- void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
- {
- static int print_prefix = 1;
- static int count;
- static char prev[LINE_SZ];
- AVBPrint part[4];
- char line[LINE_SZ];
- static int is_atty;
- int type[2];
- unsigned tint = 0;
-
- if (level >= 0) {
- tint = level & 0xff00;
- level &= 0xff;
- }
-
- if (level > av_log_level)
- return;
- ff_mutex_lock(&mutex);
-
- format_line(ptr, level, fmt, vl, part, &print_prefix, type);
- snprintf(line, sizeof(line), "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
-
- #if HAVE_ISATTY
- if (!is_atty)
- is_atty = isatty(2) ? 1 : -1;
- #endif
-
- if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
- *line && line[strlen(line) - 1] != '\r'){
- count++;
- if (is_atty == 1)
- fprintf(stderr, " Last message repeated %d times\r", count);
- goto end;
- }
- if (count > 0) {
- fprintf(stderr, " Last message repeated %d times\n", count);
- count = 0;
- }
- strcpy(prev, line);
- sanitize(part[0].str);
- colored_fputs(type[0], 0, part[0].str);
- sanitize(part[1].str);
- colored_fputs(type[1], 0, part[1].str);
- sanitize(part[2].str);
- colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[2].str);
- sanitize(part[3].str);
- colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[3].str);
-
- #if CONFIG_VALGRIND_BACKTRACE
- if (level <= BACKTRACE_LOGLEVEL)
- VALGRIND_PRINTF_BACKTRACE("%s", "");
- #endif
- end:
- av_bprint_finalize(part+3, NULL);
- ff_mutex_unlock(&mutex);
- }

- /**
- * Format a line of log the same way as the default callback.
- * @param line buffer to receive the formatted line
- * @param line_size size of the buffer
- * @param print_prefix used to store whether the prefix must be printed;
- * must point to a persistent integer initially set to 1
- */
- void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
- char *line, int line_size, int *print_prefix);
- void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
- char *line, int line_size, int *print_prefix)
- {
- av_log_format_line2(ptr, level, fmt, vl, line, line_size, print_prefix);
- }
-
- int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl,
- char *line, int line_size, int *print_prefix)
- {
- AVBPrint part[4];
- int ret;
-
- format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
- ret = snprintf(line, line_size, "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
- av_bprint_finalize(part+3, NULL);
- return ret;
- }
- /**
- * Buffer to print data progressively
- *
- * The string buffer grows as necessary and is always 0-terminated.
- * The content of the string is never accessed, and thus is
- * encoding-agnostic and can even hold binary data.
- *
- * Small buffers are kept in the structure itself, and thus require no
- * memory allocation at all (unless the contents of the buffer is needed
- * after the structure goes out of scope). This is almost as lightweight as
- * declaring a local "char buf[512]".
- *
- * The length of the string can go beyond the allocated size: the buffer is
- * then truncated, but the functions still keep account of the actual total
- * length.
- *
- * In other words, buf->len can be greater than buf->size and records the
- * total length of what would have been to the buffer if there had been
- * enough memory.
- *
- * Append operations do not need to be tested for failure: if a memory
- * allocation fails, data stop being appended to the buffer, but the length
- * is still updated. This situation can be tested with
- * av_bprint_is_complete().
- *
- * The size_max field determines several possible behaviours:
- *
- * size_max = -1 (= UINT_MAX) or any large value will let the buffer be
- * reallocated as necessary, with an amortized linear cost.
- *
- * size_max = 0 prevents writing anything to the buffer: only the total
- * length is computed. The write operations can then possibly be repeated in
- * a buffer with exactly the necessary size
- * (using size_init = size_max = len + 1).
- *
- * size_max = 1 is automatically replaced by the exact size available in the
- * structure itself, thus ensuring no dynamic memory allocation. The
- * internal buffer is large enough to hold a reasonable paragraph of text,
- * such as the current paragraph.
- */
-
- FF_PAD_STRUCTURE(AVBPrint, 1024,
- char *str; /**< string so far */
- unsigned len; /**< length so far */
- unsigned size; /**< allocated memory */
- unsigned size_max; /**< maximum allocated memory */
- char reserved_internal_buffer[1];
- )
- static void format_line(void *avcl, int level, const char *fmt, va_list vl,
- AVBPrint part[4], int *print_prefix, int type[2])
- {
- AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
- av_bprint_init(part+0, 0, AV_BPRINT_SIZE_AUTOMATIC);
- av_bprint_init(part+1, 0, AV_BPRINT_SIZE_AUTOMATIC);
- av_bprint_init(part+2, 0, AV_BPRINT_SIZE_AUTOMATIC);
- av_bprint_init(part+3, 0, 65536);
-
- if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
- if (*print_prefix && avc) {
- if (avc->parent_log_context_offset) {
- AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
- avc->parent_log_context_offset);
- if (parent && *parent) {
- av_bprintf(part+0, "[%s @ %p] ",
- (*parent)->item_name(parent), parent);
- if(type) type[0] = get_category(parent);
- }
- }
- av_bprintf(part+1, "[%s @ %p] ",
- avc->item_name(avcl), avcl);
- if(type) type[1] = get_category(avcl);
- }
-
- if (*print_prefix && (level > AV_LOG_QUIET) && (flags & AV_LOG_PRINT_LEVEL))
- av_bprintf(part+2, "[%s] ", get_level_str(level));
-
- av_vbprintf(part+3, fmt, vl);
-
- if(*part[0].str || *part[1].str || *part[2].str || *part[3].str) {
- char lastc = part[3].len && part[3].len <= part[3].size ? part[3].str[part[3].len - 1] : 0;
- *print_prefix = lastc == '\n' || lastc == '\r';
- }
- }
- /**
- * Init a print buffer.
- *
- * @param buf buffer to init
- * @param size_init initial size (including the final 0)
- * @param size_max maximum size;
- * 0 means do not write anything, just count the length;
- * 1 is replaced by the maximum value for automatic storage;
- * any large value means that the internal buffer will be
- * reallocated as needed up to that limit; -1 is converted to
- * UINT_MAX, the largest limit possible.
- * Check also AV_BPRINT_SIZE_* macros.
- */
- void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max);
- void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
- {
- unsigned size_auto = (char *)buf + sizeof(*buf) -
- buf->reserved_internal_buffer;
-
- if (size_max == 1)
- size_max = size_auto;
- buf->str = buf->reserved_internal_buffer;
- buf->len = 0;
- buf->size = FFMIN(size_auto, size_max);
- buf->size_max = size_max;
- *buf->str = 0;
- if (size_init > buf->size)
- av_bprint_alloc(buf, size_init - 1);
- }
- /**
- * Append a formatted string to a print buffer.
- */
- void av_bprintf(AVBPrint *buf, const char *fmt, ...) av_printf_format(2, 3);
- void av_bprintf(AVBPrint *buf, const char *fmt, ...)
- {
- unsigned room;
- char *dst;
- va_list vl;
- int extra_len;
-
- while (1) {
- room = av_bprint_room(buf);
- dst = room ? buf->str + buf->len : NULL;
- va_start(vl, fmt);
- extra_len = vsnprintf(dst, room, fmt, vl);
- va_end(vl);
- if (extra_len <= 0)
- return;
- if (extra_len < room)
- break;
- if (av_bprint_alloc(buf, extra_len))
- break;
- }
- av_bprint_grow(buf, extra_len);
- }
- /**
- * Append a formatted string to a print buffer.
- */
- void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg);


- int main()
- {
- printf("\e[31m Hello World. \e[0m \n"); // 红色字体
- return 0;
- }
- /**
- * 雷霄骅 Lei Xiaohua
- * leixiaohua1020@126.com
- * http://blog.csdn.net/leixiaohua1020
- */
- #include <stdio.h>
- #include <windows.h>
-
-
- int main()
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- printf("red\n");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
- printf("green\n");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE);
- printf("blue\n");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED|FOREGROUND_GREEN);
- printf("red+green=yellow\n");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED|FOREGROUND_BLUE);
- printf("red+blue=purple\n");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN|FOREGROUND_BLUE);
- printf("green+blue=cyan\n");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED|BACKGROUND_GREEN);
- printf("Add background\n");
-
-
- return 0;
- }
- static void colored_fputs(int level, int tint, const char *str)
- {
- int local_use_color;
- if (!*str)
- return;
-
- if (use_color < 0)
- check_color_terminal();
-
- if (level == AV_LOG_INFO/8) local_use_color = 0;
- else local_use_color = use_color;
-
- #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
- if (con != INVALID_HANDLE_VALUE) {
- if (local_use_color)
- SetConsoleTextAttribute(con, background | color[level]);
- win_console_puts(str);
- if (local_use_color)
- SetConsoleTextAttribute(con, attr_orig);
- } else {
- ansi_fputs(level, tint, str, local_use_color);
- }
- #else
- ansi_fputs(level, tint, str, local_use_color);
- #endif
-
- }