debug打印,后台跑,telnet上去也能看

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>

#define NONE "\033[m"
#define RED "\033[0;32;31m"
#define LIGHT_RED "\033[1;31m"
#define GREEN "\033[0;32;32m"
#define LIGHT_GREEN "\033[1;32m"
#define BLUE "\033[0;32;34m"
#define LIGHT_BLUE "\033[1;34m"
#define DARY_GRAY "\033[1;30m"
#define CYAN "\033[0;36m"
#define LIGHT_CYAN "\033[1;36m"
#define PURPLE "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN "\033[0;33m"
#define YELLOW "\033[1;33m"
#define LIGHT_GRAY "\033[0;37m"
#define WHITE "\033[1;37m"

#define SIZE (1024)

typedef struct
{
    long int type;
    char buffer[SIZE];
}Message;

int debug_print(const char* format, ...);
int debug_recv(Message* recv);
char* debug_time_str();

#define DBG(fmt, ...) do\
{\
    debug_print(GREEN"%s %d debug %s: %d: "NONE""fmt"", debug_time_str(), getpid(), __FUNCTION__, __LINE__, ##__VA_ARGS__);\
}while(0)

#define ERR(fmt, ...) do\
{\
    debug_print(RED"%s %d error %s: %d: "NONE""fmt"", debug_time_str(), getpid(), __FUNCTION__, __LINE__, ##__VA_ARGS__);\
}while(0)

#define INFO(fmt, ...) do\
{\
    debug_print(YELLOW"%s %d info %s: %d: "NONE""fmt"", debug_time_str(), getpid(), __FUNCTION__, __LINE__, ##__VA_ARGS__);\
}while(0)

#define CHECK(exp, ret, fmt...) do\
{\
    if (!(exp))\
    {\
        ERR(fmt);\
        return ret;\
    }\
}while(0)



#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "debug.h"

#define KEY (0x23581321)
#define TYPE (1)
#define MAXSENDSIZE (sizeof(Message)-sizeof(long int))

typedef struct
{
    int inited;
    int msgid;
    long int type;
    pthread_mutex_t mutex;
}msg_s;

static msg_s msg;

char* debug_time_str()
{
    static char time[32] = "";
    struct timeval tv = {0, 0};
    gettimeofday(&tv, NULL);
    strftime(time, sizeof(time), "%F %H:%M:%S", localtime(&tv.tv_sec));

    return time;
}

static int debug_init()
{
    memset(&msg, 0, sizeof(msg));
    msg.type = TYPE;
    pthread_mutex_init(&msg.mutex, NULL);
    msg.msgid = msgget(KEY, 0666|IPC_CREAT);
    if (msg.msgid == -1)
    {
        perror("create_msg failed!\n");
        return -1;
    }
    msg.inited = 1;

    return 0;
}

int debug_print(const char* format, ...)
{
    if (!msg.inited)
    {
        debug_init();
    }
    pthread_mutex_lock(&msg.mutex);
    static Message send_buf;
    memset(&send_buf, 0, sizeof(send_buf));
    send_buf.type = msg.type;

    va_list ap;
    va_start(ap, format);
    vsnprintf(send_buf.buffer, sizeof(send_buf.buffer), format, ap);
    va_end(ap);

    fprintf(stdout, "%s", send_buf.buffer);
    msgsnd(msg.msgid, &send_buf, MAXSENDSIZE, IPC_NOWAIT);
    pthread_mutex_unlock(&msg.mutex);

    return 0;
}

int debug_recv(Message* recv)
{
    if (!msg.inited)
    {
        debug_init();
    }
    pthread_mutex_lock(&msg.mutex);
    memset(recv, 0, sizeof(*recv));
    msgrcv(msg.msgid, recv, MAXSENDSIZE, msg.type, 0);
    pthread_mutex_unlock(&msg.mutex);

    return 0;
}

#include "debug.h"
#include <pthread.h>

void* proc(void* arg)
{
    int count = 1000;
    while(1)
    {
        INFO("test count: %d\n", count++);
        usleep(100*1000);
    }

    return NULL;
}

int main()
{
    int count = 0;
    pthread_t pid = 0;
    pthread_create(&pid, NULL, proc, NULL);
    while(1)
    {
        DBG("test count: %d\n", count++);
        ERR("test count: %d\n", count++);
        //INFO("test count: %d\n", count++);
        usleep(100*1000);
    }

    return 0;
}

#include "debug.h"

int main(int argc, char** argv)
{
    if (argc > 2)
    {
        printf("invalid args.argc=%d\n", argc);
        return 0;
    }

    Message recv;
    while(1)
    {
        debug_recv(&recv);
        if (argc == 2)
        {
            if (strstr(recv.buffer, argv[1]))
            {
                fprintf(stdout, "%s", recv.buffer);
            }
        }
        else
        {
            fprintf(stdout, "%s", recv.buffer);
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值