Print and output log/log to file (C + +)

Source: Internet
Author: User

#include <stdarg.h>#define Max_len 1024x768BOOLDebug_mode;//Use the same method as printfvoidlprintf (Const Char*FMT, ...) {Static BOOLPrint_time =true;//Whether to print time: When Debug_mode is true and last is the end of a newline character.     CharMessage[max_len];//Current time.time_t timer = time (NULL); Strftime (Message, at,"[%y-%m -%d %h:%m:%s] ", LocalTime (&timer));    Va_list args;    Va_start (args, FMT); VSNPRINTF (Message + A, Max_len- A, FMT, args); Va_end (args);if(Debug_mode) {printf ("%s", Message + A); }intFD = open (Log_file, o_wronly | O_creat | O_append, S_IRUSR | S_IWUSR);if(FD = =-1) {perror ("Open (log)"); }Else{if(Print_time = =false) {if(Write (FD, message + A, strlen (Message + A)) ==-1) Perror ("lprintf"); }Else{if(Write (FD, message, strlen (message)) = =-1) Perror ("lprintf"); } Print_time = (Message[strlen (message)-1] =='\ n');    Close (FD); }}

Print and output log/log to file (C + +)

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.