Skip to content

Commit

Permalink
Optimizing file log to avoid the buffered I/O layer
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Jul 28, 2015
1 parent b935e16 commit 20bb478
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions kong/plugins/filelog/log.lua
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
-- Copyright (C) Mashape, Inc.

local ffi = require "ffi"
local bit = require "bit"
local cjson = require "cjson"
local fd_util = require "kong.plugins.filelog.fd_util"
local basic_serializer = require "kong.plugins.log_serializers.basic"

ffi.cdef[[
typedef struct {
char *fpos;
void *base;
unsigned short handle;
short flags;
short unget;
unsigned long alloc;
unsigned short buffincrement;
} FILE;

FILE *fopen(const char *filename, const char *mode);
int fflush(FILE *stream);
int fprintf(FILE *stream, const char *format, ...);
int open(char * filename, int flags, int mode);
int write(int fd, void * ptr, int numbytes);
]]

local O_CREAT = 0x0200
local O_APPEND = 0x0008
local O_WRONLY = 0x0001

local S_IWUSR = 0000200
local S_IRUSR = 0000400
local S_IROTH = 0000004

local function string_to_char(str)
return ffi.cast("uint8_t*", str)
end

-- Log to a file
-- @param `premature`
-- @param `conf` Configuration table, holds http endpoint details
-- @param `message` Message to be logged
local function log(premature, conf, message)
message = cjson.encode(message).."\n"

local f = fd_util.get_fd(conf.path)
if not f then
f = ffi.C.fopen(conf.path, "a")
fd_util.set_fd(conf.path, f)
local fd = fd_util.get_fd(conf.path)
if not fd then
fd = ffi.C.open(string_to_char(conf.path), bit.bor(O_CREAT, O_APPEND, O_WRONLY), bit.bor(S_IWUSR, S_IRUSR, S_IROTH))
fd_util.set_fd(conf.path, fd)
end

ffi.C.fprintf(f, message)
ffi.C.fflush(f)
ffi.C.write(fd, string_to_char(message), string.len(message))
end

local _M = {}
Expand Down

0 comments on commit 20bb478

Please sign in to comment.