C/C++后台日志管理:实时捕获并写入文件

需积分: 10 1 下载量 89 浏览量 更新于2024-09-07 收藏 2KB TXT 举报
该资源是一个C语言代码片段,用于在Linux或Windows环境中实现将日志信息写入文件的功能。它主要包含三个函数:`get_local_time()`、`get_file_size()` 和 `write_log_file()`。 1. get_local_time()函数: 这个函数用于获取当前的本地时间并将其格式化为字符串,如 "2022-03-15 13:37:42"。它使用`time()`库函数获取系统时间,然后通过`localtime()`将时间转换为结构体`struct tm`,最后使用`sprintf()`函数将结构体中的各个时间元素格式化为字符串并存入`buffer`。 2. get_file_size()函数: 这个函数用于获取指定文件的大小(以字节为单位)。它首先打开文件,使用`fopen()`函数,如果成功则通过`fseek()`和`ftell()`移动文件指针到文件末尾,然后返回当前位置即文件大小。关闭文件后返回所得到的长度。 3. write_log_file()函数: 该核心函数负责将日志内容写入文件。参数包括文件名(`filename`)、文件最大允许大小(`max_size`)、缓冲区(`buffer`)和缓冲区大小(`buf_size`)。首先检查输入参数的有效性,然后调用`get_file_size()`获取当前文件的大小。如果文件大小已达到`max_size`,则判断是否需要追加还是覆盖旧的日志。若空间不足,可能需要先删除部分旧日志以腾出空间。接下来,函数会尝试打开文件进行写入,如果成功,使用`fprintf()`或类似函数将`buffer`中的内容写入文件,并确保不会超过`buf_size`。最后,无论操作成功与否,都会关闭文件句柄。 整个代码片段提供了一种在后台处理大量日志时,只记录关键信息并限制文件大小的方法,这对于性能优化和避免磁盘空间消耗过大非常实用。在实际应用中,可以结合条件语句或者定时任务来决定何时和如何处理和清空日志文件,以确保高效和稳定地记录关键事件。

解释下面这段cmd文件的作用:CLS REM The following is required in all INSTALL.CMD files if exist c:\system.sav\util\SetVariables.cmd Call c:\system.sav\util\SetVariables.cmd set version=1.05 Set block=%~dp0 set errcodeinstallinstallinstallinstallinstallinstallinstall=0 CD /D "%block%" set Log_Folder=%~d0\programdata\HP\logs if not exist "%Log_Folder%" md "%Log_Folder%" set Install_Log=%Log_Folder%\HotkeyInstall.log REM Remove the REM from the next line if your component does not support Silent Install (Application Recovery) REM Erase /F /Q *.CVA REM Add the command-line to have your component to be installed properly Pushd src if exist "%~dp0src\Uninstall.cmd" ( call "%~dp0src\Uninstall.cmd" ) if %errorlevel% NEQ 0 ( echo. >> "%Install_log%" echo *exit /b %errcodeinstall% >> "%Install_log%" echo. >> "%Install_log%" echo ^<^< %~f0 >> "%Install_log%" echo ^<^< %date% %time% >> "%Install_log%" echo. >> "%Install_log%" goto :END ) if exist "%~dp0src\InstallFusion.cmd" ( call "%~dp0src\InstallFusion.cmd" ) if %errorlevel% NEQ 0 ( echo. >> "%Install_log%" echo *exit /b %errcodefusion% >> "%Install_log%" echo. >> "%Install_log%" echo ^<^< %~f0 >> "%Install_log%" echo ^<^< %date% %time% >> "%Install_log%" echo. >> "%Install_log%" goto :END ) if exist "%~dp0src\InstallDriver.cmd" ( call "%~dp0src\InstallDriver.cmd" ) if %errorlevel% NEQ 0 ( echo. >> "%Install_log%" echo *exit /b %errcodedriver% >> "%Install_log%" echo. >> "%Install_log%" echo ^<^< %~f0 >> "%Install_log%" echo ^<^< %date% %time% >> "%Install_log%" echo. >> "%Install_log%"goto :END ) if exist "%~dp0src\InstallApp.cmd" ( call "%~dp0src\InstallApp.cmd" ) if %errorlevel% NEQ 0 ( echo. >> "%Install_log%" echo *exit /b %errcodeapp% >> "%Install_log%" echo. >> "%Install_log%" echo ^<^< %~f0 >> "%Install_log%" echo ^<^< %date% %time% >> "%Install_log%" echo. >> "%Install_log%" goto :END ) :END Popd REM Erase failure flag file when install succeeded. Most applications return zero to indicate success. ECHO %ERRORLEVEL% >> FAILURE.FLG IF %ERRORLEVEL% EQU 0 ERASE /F /Q FAILURE.FLG IF %ERRORLEVEL% EQU 3010 ERASE /F /Q FAILURE.FLG echo %date% %time% "Fusion=" %errcodefusion% >> %install_log% echo %date% %time% "Driver=" %errcodedriver% >> %install_log% echo %date% %time% "App=" %errcodeapp% >> %install_log% EXIT /B %ERRORLEVEL%

2023-07-20 上传
2023-06-09 上传

D:\anaconda\envs\pytorch\python.exe C:\Users\23896\Desktop\bev-lane-det_dachaung-master\tools\train_openlane.py Traceback (most recent call last): File "C:\Users\23896\Desktop\bev-lane-det_dachaung-master\tools\train_openlane.py", line 18, in <module> from torch.utils.tensorboard import SummaryWriter File "D:\anaconda\envs\pytorch\lib\site-packages\torch\utils\tensorboard\__init__.py", line 13, in <module> from .writer import FileWriter, SummaryWriter # noqa: F401 File "D:\anaconda\envs\pytorch\lib\site-packages\torch\utils\tensorboard\writer.py", line 9, in <module> from tensorboard.compat.proto.event_pb2 import SessionLog File "D:\anaconda\envs\pytorch\lib\site-packages\tensorboard\compat\proto\event_pb2.py", line 17, in <module> from tensorboard.compat.proto import summary_pb2 as tensorboard_dot_compat_dot_proto_dot_summary__pb2 File "D:\anaconda\envs\pytorch\lib\site-packages\tensorboard\compat\proto\summary_pb2.py", line 17, in <module> from tensorboard.compat.proto import tensor_pb2 as tensorboard_dot_compat_dot_proto_dot_tensor__pb2 File "D:\anaconda\envs\pytorch\lib\site-packages\tensorboard\compat\proto\tensor_pb2.py", line 16, in <module> from tensorboard.compat.proto import resource_handle_pb2 as tensorboard_dot_compat_dot_proto_dot_resource__handle__pb2 File "D:\anaconda\envs\pytorch\lib\site-packages\tensorboard\compat\proto\resource_handle_pb2.py", line 16, in <module> from tensorboard.compat.proto import tensor_shape_pb2 as tensorboard_dot_compat_dot_proto_dot_tensor__shape__pb2 File "D:\anaconda\envs\pytorch\lib\site-packages\tensorboard\compat\proto\tensor_shape_pb2.py", line 36, in <module> _descriptor.FieldDescriptor( File "D:\anaconda\envs\pytorch\lib\site-packages\google\protobuf\descriptor.py", line 561, in __new__ _message.Message._CheckCalledFromGeneratedFile() TypeError: Descriptors cannot not be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are: 1. Downgrade the protobuf package to 3.20.x or lower. 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

2023-07-20 上传

unhandled exception during asyncio.run() shutdown task: <Task finished name='Task-1' coro=<_run_app() done, defined at C:\Users\h50032875\.conda\envs\hjx\lib\site-packages\aiohttp\web.py:289> exception=OSError(10049, "error while attempting to bind on address ('10.70.6.157', 8000): 在其上下文中,该请求的地址无效。")> Traceback (most recent call last): File "C:\Users\h50032875\.conda\envs\hjx\lib\site-packages\aiohttp\web.py", line 516, in run_app loop.run_until_complete(main_task) File "C:\Users\h50032875\.conda\envs\hjx\lib\asyncio\base_events.py", line 616, in run_until_complete return future.result() File "C:\Users\h50032875\.conda\envs\hjx\lib\site-packages\aiohttp\web.py", line 415, in _run_app await site.start() File "C:\Users\h50032875\.conda\envs\hjx\lib\site-packages\aiohttp\web_runner.py", line 121, in start self._server = await loop.create_server( File "C:\Users\h50032875\.conda\envs\hjx\lib\asyncio\base_events.py", line 1463, in create_server raise OSError(err.errno, 'error while attempting ' OSError: [Errno 10049] error while attempting to bind on address ('10.70.6.157', 8000): 在其上下文中,该请求的地址无效。 Traceback (most recent call last): File "D:\motion_cap_new\motion_cap_full\webrtc_server.py", line 341, in <module> web.run_app(app, access_log=None, host=args.host, port=args.port, ssl_context=ssl_context) File "C:\Users\h50032875\.conda\envs\hjx\lib\site-packages\aiohttp\web.py", line 516, in run_app loop.run_until_complete(main_task) File "C:\Users\h50032875\.conda\envs\hjx\lib\asyncio\base_events.py", line 616, in run_until_complete return future.result() File "C:\Users\h50032875\.conda\envs\hjx\lib\site-packages\aiohttp\web.py", line 415, in _run_app await site.start() File "C:\Users\h50032875\.conda\envs\hjx\lib\site-packages\aiohttp\web_runner.py", line 121, in start self._server = await loop.create_server( File "C:\Users\h50032875\.conda\envs\hjx\lib\asyncio\base_events.py", line 1463, in create_server raise OSError(err.errno, 'error while attempting ' OSError: [Errno 10049] error while attempting to bind on address ('10.70.6.157', 8000): 在其上下文中,该请求的地址无效。

2023-07-15 上传