diff --git a/.luacheckrc b/.luacheckrc index b5652c1fca3..1eb3eaf1a7c 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -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" diff --git a/kong-0.5.4-1.rockspec b/kong-0.5.4-1.rockspec index 7a49fcf5afc..1490f30e0a1 100644 --- a/kong-0.5.4-1.rockspec +++ b/kong-0.5.4-1.rockspec @@ -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", diff --git a/kong/plugins/file-log/log.lua b/kong/plugins/file-log/log.lua index 1e1f546d30b..1249f6d124f 100644 --- a/kong/plugins/file-log/log.lua +++ b/kong/plugins/file-log/log.lua @@ -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) @@ -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))