如何在编写自定义清漆模块时记录错误?

问题描述 投票:0回答:1

我正在学习清漆以及使用内联c代码扩展清漆vmod。我开始编写自己的定制清漆模块。我想从自定义模块中记录错误和失败。我如何实现这一目标?

我可以选择从C可用的各种日志库。但我想检查是否有任何内置的清漆库来使用它。下面是我的vmod c文件的示例代码。

#include "vrt.h"
#include "cache/cache.h"
#include "vcc_if.h"
#include <jansson.h>

#define JSON_ERROR "-1"
#define JSON_LOC "/etc/example.json"


VCL_STRING
vmod_validate_mymod(VRT_CTX) {
     (void) ctx;
     char *return_code = "0";
     json_t *jobj;
     json_error_t error;
     jobj = json_load_file(JSON_LOC,0,&error);
     if (!jobj) {
          // error log here
          return JSON_ERROR;
     }
     return return_code;
}

我希望在上面代码中的if条件为true时,在自定义日志文件中添加错误日志行。请帮忙。

varnish vcl
1个回答
0
投票

你想要VSLb

VSLb(ctx->vsl, SLT_VCL_Log, "%d", 5);

如果您需要构建更大的字符串或需要分配,请使用WS_*函数,它们的分配会在rquest结束时自动释放。

看看std.log()是怎么做到的:https://github.com/varnishcache/varnish-cache/blob/master/lib/libvmod_std/vmod_std.c#L143

© www.soinside.com 2019 - 2024. All rights reserved.