c的json库不知道啥时候才有心情把它写完
我想的是先看看网络之类的再去完一下c的json库
最近又有了个新的玩具,做了一个天猫书店的抓取代码,php写的,打算把它用c重复来一遍,练练手~~
头文件
//
// log.h
// tmail_spider
//
// Created by bikang on 16/9/20.
// Copyright (c) 2016年 bikang. All rights reserved.
//
#ifndef __tmail_spider__log__
#define __tmail_spider__log__
#include
#include
#include
#include "common.h"
//error string
#define ERROR_STR() (errno == 0 ? "no error":strerror(errno))
//error
#define LOG_ERROR(M,...)\
fprintf(stderr, "[error](%s:%d,strerror:%s)"M"",__FILE__,__LINE__,ERROR_STR(),##__VA_ARGS__);
//info
#define LOG_INFO(M,...)\
fprintf(stderr, "[error](%s:%d)"M"",__FILE__,__LINE__,##__VA_ARGS__);
//error in file
void log_file(char* cate,int cate_len,const char *filename,const char *str,int len);
void log_error_file(const char *filename,const char *str,int len);
void log_info_file(const char *filename,const char *str,int len);
//error in file more data 运行传递多个参数
#endif /* defined(__tmail_spider__log__) */
实现
//
// log.c
// tmail_spider
//
// Created by bikang on 16/9/20.
// Copyright (c) 2016年 bikang. All rights reserved.
//
#include
#include
#include "log.h"
#define ERROR_STR "error|"
#define ERROR_STR_LEN 6
#define INFO_STR "info|"
#define INFO_STR_LEN 5
void log_file(char* cate,int cate_len,const char *filename,const char *str,int len){
FILE *fp = fopen(filename, "a+");
if(fp != NULL){
char date_str[30];
getLocalTime(date_str,1);
fwrite(date_str, strlen(date_str), 1, fp);
fwrite(cate, cate_len, 1, fp);
fwrite(str, len, 1, fp);
fwrite("\n", 2, 1, fp);
fclose(fp);
}
return;
}
void log_error_file(const char *filename,const char *str,int len){
log_file(ERROR_STR,ERROR_STR_LEN,filename,str,len);
}
void log_info_file(const char *filename,const char *str,int len){
log_file(INFO_STR,INFO_STR_LEN,filename,str,len);
}
公共文件
//
// common.h
// tmail_spider
//
// Created by bikang on 16/9/20.
// Copyright (c) 2016年 bikang. All rights reserved.
//
#ifndef __tmail_spider__common__
#define __tmail_spider__common__
#include
void getLocalTime(char*date_str,int flag);
#endif /* defined(__tmail_spider__common__) */
实现
//
// common.c
// tmail_spider
//
// Created by bikang on 16/9/20.
// Copyright (c) 2016年 bikang. All rights reserved.
//
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
void getLocalTime(char*date_str,int flag){
struct tm *nowdate;
time_t now;
time(&now);
nowdate = localtime(&now);
if(flag == 1){
sprintf(date_str,"%d-%02d-%02d %02d:%02d:%02d|",(nowdate->tm_year+1900),nowdate->tm_mon+1,nowdate->tm_mday,nowdate->tm_hour,nowdate->tm_min,nowdate->tm_sec);
}else{
sprintf(date_str,"%d-%02d-%02d %02d:%02d:%02d",(nowdate->tm_year+1900),nowdate->tm_mon+1,nowdate->tm_mday,nowdate->tm_hour,nowdate->tm_min,nowdate->tm_sec);
}
}
测试
//
// main.c
// tmail_spider
//
// Created by bikang on 16/9/20.
// Copyright (c) 2016年 bikang. All rights reserved.
//
#include
#include
#include
#include
#include "log.h"
#include "common.h"
//test error
void test_error();
//test log
void test_show_error();
//test file log
void test_file_error();
int main(int argc, const char * argv[]) {
//test_error();
//test_show_error();
test_file_error();
return 0;
}
void test_file_error(){
char filename[20] = "/tmp/1.txt";
char mesg[20] = "error mesg可是";
log_error_file(filename,mesg,sizeof(mesg));
log_info_file(filename,mesg,sizeof(mesg));
printf("log file end");
}
void test_show_error(){
printf("\n");
LOG_ERROR("error");
printf("\n");
LOG_INFO("info");
}
void test_error()
{
printf("error:%s\n",strerror(1));
FILE*fp;
extern int errno;
char*message;
if(NULL==(fp=fopen("/dev/dsp","r+")))
{
printf("errno=%d\n",errno);
message=strerror(errno);
printf("Mesg:%s\n",message);
}
//exit(0);
}