当前位置:网站首页>纯c语言简单的写日志文件方法,gcc编译即可
纯c语言简单的写日志文件方法,gcc编译即可
2022-08-07 06:41:00 【橘色的喵】
概述
- 这应该是之前一个C语言的模块中没有写文件的日志模块,为了不改变原先的模块编译方式,自己添加了一个简单的写日志文件的方法。
- 支持windows和linux,支持x86和ARM。
完整代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h> // linux下头文件
#endif
#define FILE_MAX_SIZE (1024*1024)
/* 获得当前时间字符串 @param buffer [out]: 时间字符串 @return 空 */
void get_local_time(char* buffer)
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
(timeinfo->tm_year+1900), timeinfo->tm_mon, timeinfo->tm_mday,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
}
/* 获得文件大小 @param filename [in]: 文件名 @return 文件大小 */
long get_file_size(char* filename)
{
long length = 0;
FILE *fp = NULL;
fp = fopen(filename, "rb");
if (fp != NULL)
{
fseek(fp, 0, SEEK_END);
length = ftell(fp);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
return length;
}
/* 写入日志文件 @param filename [in]: 日志文件名 @param max_size [in]: 日志文件大小限制 @param buffer [in]: 日志内容 @param buf_size [in]: 日志内容大小 @return 空 */
void write_log_file(char* filename, long max_size, char* buffer, unsigned buf_size)
{
if (filename != NULL && buffer != NULL)
{
// 文件超过最大限制, 删除
long length = get_file_size(filename);
if (length > max_size)
{
unlink(filename); // 删除文件
}
// 写日志
{
FILE *fp;
fp = fopen(filename, "at+");
if (fp != NULL)
{
char now[32];
memset(now, 0, sizeof(now));
get_local_time(now);
fwrite(now, strlen(now)+1, 1, fp);
fwrite(buffer, buf_size, 1, fp);
fclose(fp);
fp = NULL;
}
}
}
}
测试代码
int main(int argc, char** argv)
{
int i;
for (i=0; i<10; ++i)
{
char buffer[32];
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "====> %d\n", i);
write_log_file("log.txt", FILE_MAX_SIZE, buffer, strlen(buffer));
#ifdef WIN32
Sleep(100); // 毫秒
#else
sleep(1); // 秒
#endif
}
// system("pause");
return 0;
}
边栏推荐
- cron expression
- 360 Digital Security Brain of the Whole Network Won the "Digital Economy Innovation Leading Achievement" Award
- Ali cloud message service, SMS
- 数组扁平化
- Graphical LeetCode - 1408. String Matching in Arrays (Difficulty: Easy)
- Buffer and cache in memory
- Dark horse Cookie&Session programmers session
- 标签:对顶堆
- A Pursuit of Temporal Accuracy in General Activity Detection TAG论文阅读笔记
- Taro 路由跳转预加载
猜你喜欢

2022A Special equipment related management (elevator) special work permit test question bank simulation test platform operation

This beta version of Typora is expired

VoLTE basic self-study series | IP layer routing and addressing process in IMS network (implementation in registration process)

servlet 教程 1:环境搭建和新建 servlet 项目

VoLTE Basic Self-Learning Series | What are transparent data and non-transparent data in VoLTE?

Top 20 most popular plugins for QGIS

图解LeetCode——1408. 数组中的字符串匹配(难度:简单)

PriorityQueue(优先队列)

LeetCode's sword is Offer 06. Print the linked list from end to end

赋值、深拷贝、浅拷贝、堆和栈
随机推荐
The spyder/conda installation package reports an error: conda info could not be constructed. KeyError: 'pkgs_dirs'
什么值得买面试题(一)
DOM,SAX,JDOM,DOM4J四种方法对比总结
数组去重的几种办法
bp神经网络 损失函数,bp神经网络参数优化
Detailed explanation of fixture test fixture of pytest framework
Taro 路由跳转预加载
10年经验总结:数据分析师7种工具,因果分析划重点!
标签:对顶堆
Ali cloud message service, SMS
8 月数据库排行榜:Oracle 分数大跌,MySQL 上涨最多
How to use the @Async annotation
神经网络权值是什么意思,神经网络权重取值范围
GBL210-ASEMI机箱电源适配器整流桥GBL210
LNK2001 无法解析的外部符号 cuGetErrorName解决
VoLTE Basic Self-study Series | Summary
学神经网络需要什么基础,神经网络需要什么基础
VoLTE基础自学系列 | 什么是VoLTE中的透明数据及非透明数据?
The permutation sequence of the 60th question in C language.Breadth-first search, simple division positioning
Graphical LeetCode - 1408. String Matching in Arrays (Difficulty: Easy)