Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of C++ Code programming with examples

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "C++ code writing example analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "C++ code writing example analysis"!

The C++ code is as follows:

//log.h #ifndef _LOG_H_ #define _LOG_H_ /* LOG Library(WIN98/NT/2000) ver 0.1 Compile by: BC++ 5; C++ BUILDER 4, 5, 6, X; VC++ 5, 6; VC.NET; GCC; Copyright(c) 2006.5 - 2007.4 llbird wushaojian@21cn.com http://blog.csdn.net/wujian53 Use: This is a very simple log, using C-style functions, support multi-threading as long as the file contains, and add the log.cpp file to the project can be in VC you may need to add #include "stdafx.h" in log.cpp specific use InitLog();//initialize LOG("Program Start"); LOG1("%s", str); DestroyLog();//optional LOG_TO_STD or LOG_TO_DEBUG can be defined for debugging output LOG(Exception *e or Exception &e) can be used for C++ Builder; LOG_LAST_ERROR() for WIN32 API; LOG( _com_error &e) for_com_error; */ #include #include //use short original file name #define LOG_SHORT_SOURCE_FILE //usage log #define LOG_TO_FILE //define standard error output device #define LOG_STD_DEV stderr //use standard output device//#define LOG_TO_STD //output to debug window//#define LOG_TO_DEBUG //output messagebox //#define LOG_TO_MESSAGE_BOX //critical section for multithreading extern CRITICAL_SECTION _g_LogMutex; //global log filename extern char _g_LogFileName[MAX_PATH]; extern void InitLog(); //> initialization log extern void DestroyLog();//> cleanup log extern BOOL Log(const char* src/* source name */, int line/* line number */, const char* description/* description */);//> new log //log macro list #define LOG(arg) Log (__FILE__, __LINE__, (arg)) //multi-parameter logging macro #define LOG1(str, p1) { LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1)); LOG(buffer); } #define LOG2 (str, p1, p2) {LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2)); LOG(buffer); } #define LOG3 (str, p1, p2, p3) { LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2), (p3)); LOG(buffer); } #define LOG4 (str, p1, p2, p3, p4) { LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2), (p3), (p4));LOG(buffer);} #define LOG5 (str, p1, p2, p3, p4, p5) { LOG_SPRINTF_BUFFER; sprintf (buffer, (str), (p1), (p2), (p3), (p4), (p5)); LOG(buffer);} //log windows API error value #define LOG_LAST_ERROR() { LOG_SPRINTF_BUFFER; DWORD id = GetLastError(); sprintf(buffer, "Last Error(%d):", eid); int len = strlen(buffer); \ FormatMessage( \ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,\ NULL,\ eid, \ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \ buffer + len,\ DEFAULT_LOG_SPRINTF_BUFFER_SIZE-len-1, \ NULL \ ); \ LOG(buffer); \ }\ #if defined(__cplusplus) && defined(_INC_COMDEF) ///new COM error message inline BOOL Log(const char* src, int line, _com_error &e) { char buffer[DEFAULT_LOG_SPRINTF_BUFFER_SIZE]; sprintf(buffer, "_com_error\tCode = %x\tCode meaning = %s\tSource = %s\tDescription = %s", e.Error(), (LPCSTR)(_bstr_t)e.ErrorMessage(), (LPCSTR)(_bstr_t)e.Source(), (LPCSTR)(_bstr_t)e.Description()); return LOG_POS(src, line, buffer); } #endif ///Add VCL exception #if defined(__cplusplus) && defined(__BORLANDC__) && defined(INC_VCL) inline BOOL Log(const char* src, int line, Exception *e) { return LOG_POS(src, line, e->Message.c_str()); } inline BOOL Log(const char* src, int line, Exception &e) { return LOG_POS(src, line, e.Message.c_str()); } #endif #endif _LOG_H_At this point, I believe that everyone has a deeper understanding of "C++ code writing example analysis", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 284

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report