c输出log文件|LINUX下C语言编程怎么打印日志

|

㈠ C语言中log函数怎么使用啊

1、C语言中,有两个log函数,分别为log10和log函数,具体用法如下:2、函数名: log10功 能: 对数函数log,以10为底用 法: double log10(double x);程序示例:#include <math.h>#include <stdio.h>int main(void){ double result; double x = 800.6872; result = log10(x); printf("The common log of %lf is %lf\n", x, result); return 0;} 3、函数名: log功 能: 对数函数log,以e(2.71828)为底用 法: double log(double x);程序示例:#include <math.h>#include <stdio.h>int main(void){ double result; double x = 800.6872; result = log(x); printf("The common log of %lf is %lf\n", x, result); return 0;}

㈡ 在c语言中log怎么输入

原型:double log (double x); 头文件:math.h 功能:计算以e 为底的对数值 程序例: #include <math.h> #include <stdio.h> int main(void){double result; double x = 321.123; result = log(x); printf("The common log of %lf is %lf\n", x, result); return 0; }C语言里面有该函数,所以输入一个双精度浮点数,对其进行函数变换即可生成其对数。还有如果你的意思是输入对数进行幂运算的话有下面这个函数原型:extern float pow(float x, float y);用法:#include <math.h> 功能:计算x的y次幂。 说明:x应大于零,返回幂指数的结果。 举例: // pow.c #include <stdlib.h> #include <math.h> #include <conio.h> void main() { printf("4^5=%f",pow(4.,5.)); getchar(); }

㈢ c语言怎么写log日志

#include<stdio.h>#include<stdarg.h>#include<time.h>intwrite_log(FILE*pFile,constchar*format,…){va_listarg;intdone;va_start(arg,format);//done=vfprintf(stdout,format,arg);time_ttime_log=time(NULL);structtm*tm_log=localtime(&time_log);fprintf(pFile,"%04d-%02d-%02d%02d:%02d:%02d",tm_log->tm_year+1900,tm_log->tm_mon+1,tm_log->tm_mday,tm_log->tm_hour,tm_log->tm_min,tm_log->tm_sec);done=vfprintf(pFile,format,arg);va_end(arg);fflush(pFile);returndone;}

㈣ 如何查看log日志文件呢

查看方法:一、在 Java 与 C 语言中输出日志:1) Java 代码在程序中输出日志, 使用 android.util.Log 类的以下 5 个方法: Log.v()、Log.d()、Log.i()、Log.w()、Log.e()。 分对应 Verbose、Debug、INFO、Warn、Error 的首字母。 例如:Log.i( "类::函数名", "日期_时间_源码文件名_行号_日志信息内容" );2) C 代码在程序中输出日志,使用 log 的 API 函数: __android_log_write( 日志类型宏,日志标签字符串,日志令牌内容字符串 ); 需要:1. Android.mk 中添加 LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 2. *.c 中添加 #include <android/log.h> 3. 日志类型宏有:复制代码 代码如下: // Android log priority values, in ascending priority order. typedef enum android_LogPriority { ANDROID_LOG_UNKNOWN = 0, // only for SetMinPriority() ANDROID_LOG_DEFAULT, ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, // only for SetMinPriority(); must be last ANDROID_LOG_SILENT, } android_LogPriority;二、logcat 使用方法: Usage: logcat [options] [filterspecs]用法: logcat [选项] [过滤说明]options include:选项包含: -s Set default filter to silent. Like specifying filterspec '*:S' 设置默认过滤为无声的。 像指定过滤说明为 *:S ,见下面 过滤说明 部份详述 -f <filename> Log to file. Default to stdout 输出日志到文件。 默认为 stdout -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f 设置环形日志缓冲区的kbytes。 默认值为16。 需要和 -f 选项一起使用 -n <count> Sets max number of rotated logs to <count>, default 4 设置环形日志缓冲区的最大数目,默认值是4,需要和 -r 选项一起使用 -v <format> Sets the log print format, where <format> is one of: 设置 log 的打印格式, 格式有如下主要7种:(不能组合使用)brief process tag thread raw time threadtime long-c clear (flush) the entire log and exit 清除所有 log 并退出 -d mp the log and then exit (don't block) 得到所有log并退出且不阻塞 -t <count> print only the most recent <count> lines (implies -d) 仅打印最近的由参数 count 指出的行数(必然包含 -d) -g get the size of the log's ring buffer and exit 得到环形缓冲区的大小并退出 -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio' or 'events'. Multiple -b parameters are allowed and the results are interleaved. The default is -b main -b system. 请求供替换的环形缓冲区,如:main,system,radio,events。 多个 -b 参数是被允许,并且结果是交错输出的。 -b main -b system 是默认的。 -B output the log in binary 输出 log 到二进制文件中。filterspecs are a series of <tag>[:priority]过滤说明是一系列 <tag>[:priority]where <tag> is a log component tag (or * for all) and priority is:tag 是 eclipse 中 logcat 图形界面中 Tag 的内容(或者有 * 表示全部),它之后的冒号(:)后面跟优先级: 日志类型标识符(优先级由低到高排列): 1. V — Verbose 详细的 <- 最低优先权 2. D — Debug 调试 3. I — Info 消息 4. W — Warn 警告 5. E — Error 错误 6. F — Fatal 致命的 7. S — Silent 无声的 <- 最高优先权'*' means '*:d' and <tag> by itself means <tag>:v* 意味着 *:d 且 单孤地 tag 意味着 tag:VIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.如果在命令行上没有详细说明,过滤规格即是 ANDROID_LOG_TAGS 结果集。If no filterspec is found, filter defaults to '*:I'如果没有过滤说明,过滤规格默认为 *:IIf not specified with -v, format is set from ANDROID_PRINTF_LOG or defaults to "brief"如果没有 -v 指定格式,将是 ANDROID_PRINTF_LOG 或 brief 格式集。1) 只输出指定 标签 和 类型 的日志 格式: adb logcat <日志标签>:<日志类型标识符> <日志标签>:<日志类型标识符> … *:S 注:1. 可以写多个 <日志标签>:<日志类型标识符> 之间用空格分隔; 2. 最后必须是 *:S ,表示其它的都不要显示出来 例如: $ adb logcat dalvikvm:D Checkin:W *:S 注:adb logcat Checkin *:S =等同于=> adb logcat Checkin:V *:S 注:以上命令均没加 -v 来指出日志格式,即默认为: ANDROID_PRINTF_LOG 或 brief 格式集。2) 输出指定 标签 和 类型 的带有格式的日志注:以下测试日志内容为:test log format,即 eclipse 中的 logcat 图形界面里的 Text 中的内容!1. brief – 日志类型/日志标签(进程ID): 日志内容 例如:$ adb logcat -v brief Checkin *:S I/Checkin(24713): test log format 2. process – 日志类型(进程ID) 日志内容 (日志标签) 例如:$ adb logcat -v process Checkin *:S I(24713) test log format (Checkin) 3. tag – 日志类型/日志标签: 日志内容 例如:$ adb logcat -v tag Checkin *:S I/Checkin: test log format4. thread – 日志类型(进程ID:线程ID) 例如:$ adb logcat -v thread Checkin *:S I(24713:0x6089) test log format5. raw – 日志内容 例如:$ adb logcat -v raw Checkin *:S test log format6. time – 日期 调用时间 日志类型/日志标签(进程ID): 日志内容 例如:$ adb logcat -v time Checkin *:S 05-27 11:25:33.854 I/Checkin(24713): test log format7. threadtime – 日期 调用时间 进程ID 线程ID 日志类型 日志标签: 日志内容 例如:$ adb logcat -v time Checkin *:S 05-27 11:25:33.854 24713 24713 I Checkin: test log format 注:只有此种格式时 线程ID 为十进制数。8. long – [ 日期 调用时间 进程ID:线程ID 日志类型/日志标签 ] 转行显示 日志内容 例如:$ adb logcat -v long Checkin *:S [ 05-27 11:25:33.854 24713:0x6089 I/Checkin ] test log format

㈤ 求一个C语言,txt文件输出宏,想记录程序运行log

#define PRINTSTR(a) {FILE *p;p=fopen("outing.txt","w"); fprintf(p,a);fclose(p);}可行吗?

㈥ LINUX下C语言编程怎么打印日志

我们的程序一般都会产生输出信息。但是服务器程序一般却不希望输出信息到屏幕上,因为没有人盯着你的程序执行。所以我们要把一些信息写成日志文件,正常情况下运行程序的人不用关心日志里的内容,只有在出现问题的时候才会查看日志文件里的内容以确定问题所在。但如果我们的程序要自己生成一个文件来保存日志却不是好主意,因为这一方面增加了维护程序运行的人的负担,另一方面自己维护起系统来也多有不便。在Linux系统中有一个系统日志,通常放在/var/log目录下,比如文件名是syslog的,系统中的一些程序产生的日志信息都会存放到这个文件里。日志文件有固定的格式,比如第1列是消息产生的时间,第2列是机器名(因为日志记录程序支持远程连接),第3列是标记信息(一般就是程序名称)等。而且对应的有一些工具来对这个日志进行维护,比如通过轮回机制保证日志文件大小不会把磁盘空间占尽。所以我们把自己程序的信息也写到这个系统日志里是比较好的想法。在GNU C语言库提供的内容中,有接口可以用来做这件事。用下面的命令查看:

nm -D /lib/libc.so.6 | grep log

可以看到一些调用:

000b9410Tcloselog0008b870Tgetlogin0008b960Tgetlogin_r000d0180T__getlogin_r_chk000bd190Tklogctl00027450T__open_catalog000b9380Topenlog0008bae0Tsetlogin000b8b80Tsetlogmask000b9350Tsyslog000b9320T__syslog_chk000b92f0Tvsyslog000b8da0T__vsyslog_chk

这里面的三个函数openlog, syslog, closelog是一套系统日志写入接口。另外那个vsyslog和syslog功能一样,只是参数格式不同。程序的用法示例代码如下:

#include<syslog.h>intmain(intargc,char**argv){openlog("MyMsgMARK",LOG_CONS|LOG_PID,0);syslog(LOG_DEBUG,"'%s'",argv[0]);closelog();return0;}

编译生成可执行程序后,运行一次程序将向/var/log/syslog文件添加一行信息如下:

Feb1208:48:38localhostMyMsgMARK[7085]:'./a.out'

Feb 12 08:48:38 localhost MyMsgMARK[7085]: This is a syslog test message generated by program './a.out'

LOG_CONS.LOG_NDELAYOpentheconnectionimmediately(normally,).LOG_NOWAITDon’.(TheGNUClibrarydoesnotcreateachildprocess,.)LOG_ODELAYTheconverseofLOG_NDELAY;()iscalled.(Thisisthedefault,andneednotbespecified.)LOG_PERROR(NotinSUSv3.)Printtostderraswell.LOG_PIDIncludePIDwitheachmessage.

第三个参数指明记录日志的程序的类型。syslog函数及参数syslog函数用于把日志消息发给系统程序syslogd去记录,此函数原型是:void syslog(int priority, const char *format, …);第一个参数是消息的紧急级别,第二个参数是消息的格式,之后是格式对应的参数。就是printf函数一样使用。如果我们的程序要使用系统日志功能,只需要在程序启动时使用openlog函数来连接syslogd程序,后面随时用syslog函数写日志就行了。另外,作为syslog的替代程序的新一代工具是syslog-ng,syslog-ng具有很强的网络功能,可以方便地把多台机器上的日志保存到一台中心日志服务器上。

㈦ 编程一个程序,输出c盘windows目录下的任意一个log文件的内容

用Dos命令就可以

㈧ C语言怎样将结果输出到文件中

第一种#define printf(fmt,…) fprintf(openedfile,fmt,__VA_ARGS__)然后在你第一次用printf之前,打开文件就可以了。第二中在第一次printf之前freopen("c:\\log.txt","w+",stdout)

㈨ 求LINUX下,C语言编写的日志输出源码~

#include <stdlib.h>#include <string.h>#include <stdio.h>#include <dirent.h>#include <time.h>#define LOGFILE “./dir_log_0″int g_Count;//#define MAXLEN 1024void WriteDebugLog(char *str);int main(int argc, char **argv){char str[1024]={0};strcpy(str,”file no find”);int i=0,j=0;for (i=0; i<10; i++){for (j=0; j<50; j++){WriteDebugLog(str);}}return 0;}void WriteDebugLog(char *str){char buf[2048]={0};char logFileName[50]={0};//long MAXLEN = 50*1024*1024;//50MBint iMax = 1024;//1Ktime_t timep;FILE *fp = NULL;struct tm *p;time(&timep);p = localtime(&timep);memset(buf,0,sizeof(buf));sprintf(buf,”[%d-%d-%d %d:%d:%d][DEBUG]”,(1900+p->tm_year),(1+p->tm_mon), p->tm_mday,p->tm_hour, p->tm_min, p->tm_sec); //星期p->tm_wdaystrcat(buf,str);strcat(buf,””);strcpy(logFileName,LOGFILE);int len = strlen(logFileName);logFileName[len-1] = &#390&#39+g_Count;fp = fopen(logFileName,”r”);if(fp==NULL){fp = fopen(logFileName,”w+”);}else{fseek(fp,0,2);//SEEK_END值为if( ftell(fp) >= iMax){fclose(fp);if (g_Count >= 9){logFileName[len-1] = &#390'g_Count=0;}else{g_Count++;logFileName[len-1] = &#390&#39+g_Count;// printf(“%c”,&#390&#39+g_Count);}fp = fopen(logFileName,”w+”);}else{fclose(fp);fp = fopen(logFileName,”a”);}}fwrite(buf,1,strlen(buf),fp);fclose(fp);}


赞 (0)