Skip to content

Commit

Permalink
Merge pull request #787 from Mashape/chore/file-log
Browse files Browse the repository at this point in the history
Better handling of system constants in the file-log plugin
  • Loading branch information
subnetmarco committed Dec 8, 2015
2 parents 1a57aba + ea03488 commit 2180dbf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
redefined = false
unused_args = false
globals = {"ngx", "dao", "app", "configuration"}
globals = {"ngx", "dao", "app", "configuration", "process_id"}

files["kong/"] = {
std = "luajit"
Expand Down
1 change: 1 addition & 0 deletions kong-0.5.4-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = {
"luasec ~> 0.5-2",

"lua_uuid ~> 0.1-8",
"lua_system_constants ~> 0.1-2",
"luatz ~> 0.3-1",
"yaml ~> 1.1.2-1",
"lapis ~> 1.3.1-1",
Expand Down
25 changes: 12 additions & 13 deletions kong/plugins/file-log/log.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
-- Copyright (C) Mashape, Inc.

local ffi = require "ffi"
local bit = require "bit"
local cjson = require "cjson"
local fd_util = require "kong.plugins.file-log.fd_util"
local system_constants = require "lua_system_constants"
local basic_serializer = require "kong.plugins.log-serializers.basic"

ffi.cdef[[
int open(char * filename, int flags, int mode);
int write(int fd, void * ptr, int numbytes);
]]

local octal = function(n) return tonumber(n, 8) end

local O_CREAT = octal('0100')
local O_APPEND = octal('02000')
local O_WRONLY = octal('0001')

local S_IWUSR = octal('00200')
local S_IRUSR = octal('00400')
local S_IXUSR = octal('00100')
char *strerror(int errnum);
]]

local function string_to_char(str)
return ffi.cast("uint8_t*", str)
Expand All @@ -34,8 +26,15 @@ local function log(premature, conf, message)

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_IXUSR))
fd_util.set_fd(conf.path, fd)
fd = ffi.C.open(string_to_char(conf.path),
bit.bor(system_constants.O_WRONLY(), system_constants.O_CREAT(), system_constants.O_APPEND()),
bit.bor(system_constants.S_IWUSR(), system_constants.S_IRUSR(), system_constants.S_IXUSR()))
if fd < 0 then
local errno = ffi.errno()
ngx.log(ngx.ERR, "[file-log] failed to open the file: ", ffi.string(ffi.C.strerror(errno)))
else
fd_util.set_fd(conf.path, fd)
end
end

ffi.C.write(fd, string_to_char(message), string.len(message))
Expand Down

0 comments on commit 2180dbf

Please sign in to comment.