C Program implementation for obtaining code file name, line number and date time under Linux

Source: Internet
Author: User

I. Overview
In the actual software development project, in order to facilitate the troubleshooting of the program, the log file is required to output the program file name and the number of lines where the log code is located. In addition, some software requires that the date and time of the program's startup be output to a log file for easy tracking of software health.
This paper introduces the implementation of C code for obtaining code file name, code line number and date time under Linux.

II. Introduction to several standard predefined macros
In the C language, the ability to get code filenames, lines of code, and DateTime can be easily achieved with several standard predefined macros.
These macros are defined as follows (note: Both front and back are two consecutive underscores):

__FILE__:在源文件中插入当前源文件名。__LINE__:在源代码中插入当前源代码行号。__DATE__:在源文件中插入当前的编译日期。__TIME__:在源文件中插入当前编译时间。

Third, C code implementation

/*********************************************************************** All rights reserved (C), Zhou Zhaoxiong.** File name: filenameandline.c* File ID: None* Content Summary: The code name of the printed output, the number of rows and the date time* Other instructions: None* Current version: V1.0* Author: Zhou Zhaoxiong* Completion Date: 20150511***********************************************************************/#include <stdio.h>Redefine data type typedef signed INT int32;typedef unsigned char uint8;/*********************************************************************** Function Description: main function* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* -------------------------------------------------------------------* 20150511 V1.0 Zhou Zhaoxiong created***********************************************************************/int32 Main () {UINT8 szcontentbuf[256] = {0};snprintf (szcontentbuf, sizeof (SZCONTENTBUF)-1, "This log was in [%s], and Lineno are [%d], currenttime is [%s%s].\n ", __file__, __line__, __date__, __time__);printf (SZCONTENTBUF); Output a messagereturn 0;}

Iv. compilation of files and results of operation
Execute "gcc-g-o filenameandline filenameandline.c" command under Linux to generate "Filenameandline". Then execute the "filenameandline" command, the results of the program run as follows:

[zhou|/home/zhou/zhouzx/testFileNameAndLineFileNameAndLine.c[zhou|/home/zhou/zhouzx/testFileNameAndLineThisin [FileNameAndLineand lineno is [33], currenttime is [May11201514:22:34].

My public number: ZHOUZXI, please scan the following two-dimensional code:

C Program implementation for code file name, line number and date time under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.