在Linux系统中(32位或64位),什么是大小pid_t,uid_t和gid_t?
我有一些需要经常打印的结构.现在,我正在使用这个结构的经典打印包装器:
void printf_mystruct(struct* my_struct)
{
if (my_struct==NULL) return;
printf("[value1:%d value2:%d]", struct->value1, struct->value2);
}
Run Code Online (Sandbox Code Playgroud)
这个功能很方便,但也非常有限.我不能在不制作新包装的情况下预先添加或附加一些文本.我知道我可以使用va_arg系列来预装或推出一些文本,但我觉得我会重新实现这个轮子.
我想知道是否可以为printf编写自定义功能.我希望能够写出这样的东西:
register2printf("%mys", &printf_mystruct);
...
if (incorrect)
printf("[%l] Struct is incorrect : %mys\n", log_level, my_struct);
Run Code Online (Sandbox Code Playgroud)
这可能吗 ?我怎样才能做到这一点 ?
注意:我在Ubuntu Linux 10.04下使用gcc.
我想创建一个自定义的打印函数,用法与 printf 相同,但为 pid 和 tid 添加 2 个参数。
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/syscall.h>
int gettid() {
return syscall(__NR_gettid);
}
void print( const char *fmt,...) {
va_list vlist;
va_start(vlist, fmt);
va_end(vlist);
return;
}
int main() {
printf("%d %d hello\n",getpid(), gettid());
//print("hello\n");
return 0;
Run Code Online (Sandbox Code Playgroud)
实现打印功能的正确方法是什么?