主题
获取当前文件大小 - LogGetCurrentFileSize
函数简介
获取当前日志文件的大小(字节)。
接口名称
LogGetCurrentFileSizeDLL调用
c
int64_t LogGetCurrentFileSize(int64_t instance, int64_t loggerHandle);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| loggerHandle | 长整数型 | 日志实例句柄(0 表示默认实例)。 |
示例
写日志后查询当前文件大小(字节)。
SDK 调用
cpp
#include "OLAPlugServer.h"
#include <cstdio>
OLAPlugServer ola;
const int64_t logger = 0; // 默认实例
ola.LogSetFilePath(logger, "D:\\logs\\app.log");
ola.LogInfo("size probe line");
ola.LogFlush(logger);
int64_t size = ola.LogGetCurrentFileSize(logger);
std::printf("current file size(bytes): %lld\n", (long long)size);csharp
using System;
using OLAPlug;
var ola = new OLAPlugServer();
long logger = 0;
ola.LogSetFilePath(logger, @"D:\logs\app.log");
ola.LogInfo("size probe line");
ola.LogFlush(logger);
long size = ola.LogGetCurrentFileSize(logger);
Console.WriteLine("current file size(bytes): " + size);python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
logger = 0
ola.LogSetFilePath(logger, r"D:\logs\app.log")
ola.LogInfo("size probe line")
ola.LogFlush(logger)
size = ola.LogGetCurrentFileSize(logger)
print("current file size(bytes):", size)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
long logger = 0;
ola.LogSetFilePath(logger, "D:\\logs\\app.log");
ola.LogInfo("size probe line");
ola.LogFlush(logger);
long size = ola.LogGetCurrentFileSize(logger);
System.out.println("current file size(bytes): " + size);go
import (
"fmt"
"github.com/ola/olaplug/olaplug"
)
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
logger := int64(0)
ola.LogSetFilePath(logger, `D:\logs\app.log`)
ola.LogInfo("size probe line")
ola.LogFlush(logger)
size := ola.LogGetCurrentFileSize(logger)
fmt.Println("current file size(bytes):", size)rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let logger = 0i64;
ola.log_set_file_path(logger, r"D:\logs\app.log");
ola.log_info("size probe line");
ola.log_flush(logger);
let size = ola.log_get_current_file_size(logger);
println!("current file size(bytes): {}", size);cpp
var ola = com("OlaPlug.OlaSoft")
var logger = 0
ola.LogSetFilePath(logger, "D:\\logs\\app.log")
ola.LogInfo("size probe line")
ola.LogFlush(logger)
var size = ola.LogGetCurrentFileSize(logger)
traceprint("current file size(bytes): " + size)vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
logger = 0
ola.LogSetFilePath logger, "D:\logs\app.log"
ola.LogInfo "size probe line"
ola.LogFlush logger
size = ola.LogGetCurrentFileSize(logger)
TracePrint "current file size(bytes): " & sizetext
.局部变量 ola, OLAPlug
.局部变量 ret, 整数型
.局部变量 logger, 长整数型
.局部变量 size, 长整数型
ola.Create ()
logger = 0
ola.LogSetFilePath (logger, “D:\logs\app.log”)
ola.LogInfo (“size probe line”)
ola.LogFlush (logger)
size = ola.LogGetCurrentFileSize (logger)
调试输出 (“current file size(bytes):”, size)aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var logger = 0;
ola.LogSetFilePath(logger, "D:\\logs\\app.log");
ola.LogInfo("size probe line");
ola.LogFlush(logger);
var size = ola.LogGetCurrentFileSize(logger);
io.print("current file size(bytes): ", size);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 logger = 0
ola.LogSetFilePath(logger, "D:\\logs\\app.log")
ola.LogInfo("size probe line")
ola.LogFlush(logger)
长整数 size = ola.LogGetCurrentFileSize(logger)
调试输出 ("current file size(bytes):", size)cpp
#include "OLAPlugServer.h"
#include <cstdio>
OLAPlugServer ola;
int64_t logger = 0;
ola.LogSetFilePath(logger, "D:\\logs\\app.log");
ola.LogInfo("size probe line");
ola.LogFlush(logger);
int64_t size = ola.LogGetCurrentFileSize(logger);
std::printf("current file size(bytes): %lld\n", (long long)size);原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long logger = 0;
LogSetFilePath(instance, logger, "D:\\logs\\app.log");
LogInfo(instance, "size probe line");
LogFlush(instance, logger);
int64_t size = LogGetCurrentFileSize(instance, logger);csharp
using System.Runtime.InteropServices;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int LogSetFilePath(long ola, long loggerHandle, string logFilePath);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int LogInfo(long ola, string message);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int LogFlush(long ola, long loggerHandle);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long LogGetCurrentFileSize(long ola, long loggerHandle);
long instance = CreateCOLAPlugInterFace();
long logger = 0;
LogSetFilePath(instance, logger, @"D:\logs\app.log");
LogInfo(instance, "size probe line");
LogFlush(instance, logger);
long size = LogGetCurrentFileSize(instance, logger);python
from ctypes import CDLL, c_int, c_int64
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
logger = 0
ola.LogSetFilePath(instance, logger, b"D:\\logs\\app.log")
ola.LogInfo(instance, b"size probe line")
ola.LogFlush(instance, logger)
print("current file size(bytes):", ola.LogGetCurrentFileSize(instance, logger))返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 文件大小(长整数型,单位:字节),失败返回 -1。 |
注意事项
| 项目 | 说明 |
|---|---|
| 返回值单位是字节(Byte) | 返回值单位是字节(Byte)。 |
| 如果日志系统未初始化 | 如果日志系统未初始化,返回 -1。 |
| 文件大小会随着日志写入而增长 | 文件大小会随着日志写入而增长。 |
达到 LogSetMaxFileSize 设 | 达到 LogSetMaxFileSize 设置的大小时会自动分割。 |
| 可以用于监控日志文件大小 | 可以用于监控日志文件大小,实现自定义的分割策略。 |
