今天写一个日志类,不过目前还有bug。condition的destroy不知道怎么的死锁了,之前测试很好的,并入其他模块就不能工作了,我得想办法调试一下,毕竟多线程程序不好调试。
先来分析一下今天用的这几个函数,其实以前看libevent,muduo这些函数都出现过,不过学了又忘了,这次总结一下。
1.valist
C语言标准库中头文件stdarg.h声明了一组能够变长参数的宏。主要有:
1、va_list 用来声明一个表示参数表中各个参数的变量。
2、va_start 初始化一个指针来指向变长参数列表的头一个变量(注意,...只能出现在参数表的最后)
3、va_arg每次调用时都会返回当前指针指向的变量,并将指针挪至下一个位置,参数的类型需要在这个调用的第二个参数来指定,va_arg也是根据这个参数来判断偏移的距离。
4、va_end需要在函数最后调用,来进行一些清理工作。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <iostream> #include <stdio.h> #include <string.h> #include <stdarg.h> void print(const char* format, ...) { va_list vlist; va_start(vlist, format); char buf[1024]; memset(buf, 0, sizeof(buf)); vsprintf(buf, format, vlist); printf("%sn", buf); va_end(vlist); } int main() { const char *s1 = "hello,"; const char *s2 = "world."; print("%s%s", s1, s2); return 0; }
2.snprintf
1snprintf(),为函数原型int snprintf(char *str, size_t size, const char *format, ...)。
(1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('');
(2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符(''),返回值为欲写入的字符串长度。
所以说,valist和snprintf是天生一对,天衣无缝的配合。
3.vsnprintf和snprintf是C语言printf家族函数成员,相关函数列表如下:
1
2
3
4
5
6
7
8
9
10
11
12#include <stdio.h> int printf(const char *format, ...); //输出到标准输出 int fprintf(FILE *stream, const char *format, ...); //输出到文件 int sprintf(char *str, const char *format, ...); //输出到字符串str中 int snprintf(char *str, size_t size, const char *format, ...); //按size大小输出到字符串str中 以下函数功能与上面的一一对应相同,只是在函数调用时,把上面的...对应的一个个变量用va_list调用所替代。在函数调用前ap要通过va_start()宏来动态获取。 #include <stdarg.h> int vprintf(const char *format, va_list ap); int vfprintf(FILE *stream, const char *format, va_list ap); int vsprintf(char *str, const char *format, va_list ap); int vsnprintf(char *str, size_t size, const char *format, va_list ap);
例:通过vsnprintf()实现snprintf()功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <stdio.h> #include <stdarg.h> int my_snprintf(char *s, int size, const char *fmt, ...) //该自定义函数,与系统提供的snprintf()函数相同。 { va_list ap; int n=0; va_start(ap, fmt); //获得可变参数列表 n=vsnprintf (s, size, fmt, ap); //写入字符串s va_end(ap); //释放资源 return n; //返回写入的字符个数 } int main() { char str[1024]; my_snprintf( str, sizeof(str), "%d,%d,%d,%d",5,6,7,8); printf("%sn",str); return 0; }
4.localtime函数
1
2int gettimeofday(struct timeval *tv, struct timezone *tz);
1
2
3
4
5struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ };
1
2
3
4
5
6
7int64_t get_current_timestamp() { struct timeval now = {0, 0}; gettimeofday(&now, NULL); return now.tv_sec * 1000 * 1000 + now.tv_usec; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <iostream> #include <time.h> #include <sys/time.h> using namespace std; int main() { struct timeval now = {0, 0}; gettimeofday(&now, NULL); time_t t = now.tv_sec; struct tm* sys_tm = localtime(&t); struct tm my_tm = *sys_tm; return 0; }
用GDB即可看到:
5.strchr()函数:查找某一字符在字符串中最后一次出现的位置
比如在 "./path" 中查找'/',这样他就会返回'/'所在位置的下标p,我们用p+1,就可以的到path名称了。
6.fstream
使用如下组合来打开文件,并检测是否真的打开。
记住:使用fstream一定要包含头文件#include <fstream>,只有iostream是不管用的。
最后
以上就是开心方盒最近收集整理的关于valist、vsnprintf、snprintf、localtime分析的全部内容,更多相关valist、vsnprintf、snprintf、localtime分析内容请搜索靠谱客的其他文章。
发表评论 取消回复