Skip to content

Fix log level and error handling #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 29 additions & 31 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright (c) 2024 Erich L Foster
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -20,6 +20,31 @@

FROM ubuntu:22.04 as builder

# Install dependencies needed for building devcontainers/cli and developing in neovim
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
apt-utils \
build-essential \
curl \
wget \
nodejs \
npm \
lua5.1 \
luajit \
luarocks \
git \
# apt clean-up
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Installing the devcontainers CLI
RUN npm install -g @devcontainers/[email protected]

# Installing Lua Dependencies for testing LUA projects
RUN luarocks install busted

ENV USER_NAME=my-app
ARG GROUP_NAME=$USER_NAME
ARG USER_ID=1000
Expand All @@ -31,37 +56,10 @@ RUN groupadd --gid $GROUP_ID $GROUP_NAME && \
&& apt-get update \
&& apt-get install -y --no-install-recommends sudo \
&& echo $USER_NAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USER_NAME \
&& chmod 0440 /etc/sudoers.d/$USER_NAME
&& chmod 0440 /etc/sudoers.d/$USER_NAME

# Switch to user
USER $USER_NAME

# Install dependencies needed for building devcontainers/cli and developing in neovim
RUN sudo apt-get update && \
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
apt-utils \
build-essential \
curl \
wget \
nodejs \
npm \
lua5.1 \
luajit \
luarocks \
git \
# apt clean-up
&& sudo apt-get autoremove -y \
&& sudo rm -rf /var/lib/apt/lists/*

ENV NPM_CONFIG_PREFIX=/home/$USER_NAME/.npm-global

WORKDIR /app

# Installing the devcontainers CLI
RUN npm install -g @devcontainers/[email protected]

# Installing Lua Dependencies for testing LUA projects
RUN sudo luarocks install busted

# this will prevent the .local directory from being owned by root on bind mount
RUN mkdir -p /home/$USER_NAME/.local/share/nvim/lazy
37 changes: 19 additions & 18 deletions lua/devcontainer-cli/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local global_config = require("devcontainer-cli.config")
-- User configuration section
local default_config = {
-- Name of the plugin. Prepended to log messages
plugin = 'decontainer-cli',
plugin = "devcontainer-cli",

-- Should print the output to neovim while running
use_console = true,
Expand All @@ -29,12 +29,12 @@ local default_config = {

-- Level configuration
modes = {
{ name = "trace", hl = "Comment", },
{ name = "debug", hl = "Comment", },
{ name = "info", hl = "None", },
{ name = "warn", hl = "WarningMsg", },
{ name = "error", hl = "ErrorMsg", },
{ name = "fatal", hl = "ErrorMsg", },
{ name = "trace", hl = "Comment" },
{ name = "debug", hl = "Comment" },
{ name = "info", hl = "None" },
{ name = "warn", hl = "WarningMsg" },
{ name = "error", hl = "ErrorMsg" },
{ name = "fatal", hl = "ErrorMsg" },
},

-- Can limit the number of decimals displayed for floats
Expand All @@ -49,7 +49,7 @@ local unpack = unpack or table.unpack
log.new = function(config, standalone)
config = vim.tbl_deep_extend("force", default_config, config)

local outfile = string.format('%s/%s.log', vim.fn.stdpath("cache"), config.plugin)
local outfile = string.format("%s/%s.log", vim.fn.stdpath("cache"), config.plugin)

local obj
if standalone then
Expand All @@ -66,12 +66,12 @@ log.new = function(config, standalone)
local round = function(x, increment)
increment = increment or 1
x = x / increment
return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment
return (x > 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)) * increment
end

local make_string = function(...)
local t = {}
for i = 1, select('#', ...) do
for i = 1, select("#", ...) do
local x = select(i, ...)

if type(x) == "number" and config.float_precision then
Expand All @@ -87,7 +87,6 @@ log.new = function(config, standalone)
return table.concat(t, " ")
end


local log_at_level = function(level, level_config, message_maker, ...)
local nameupper = level_config.name:upper()

Expand Down Expand Up @@ -120,13 +119,15 @@ log.new = function(config, standalone)
end

-- Output to log file
if config.use_file and level >= levels[config.log_level] then
local fp = io.open(outfile, "a")
local str = string.format("[%-6s%s] %s: %s\n",
nameupper, os.date(), lineinfo, msg)
if fp ~= nil then
if config.use_file and level < levels[config.log_level] then
local ok, err = pcall(function()
local fp = assert(io.open(outfile, "a"))
local str = string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg)
fp:write(str)
fp:close()
end)
if not ok then
vim.notify(string.format("Failed to write to log file: %s", err), vim.log.levels.ERROR)
end
end
end
Expand All @@ -136,9 +137,9 @@ log.new = function(config, standalone)
return log_at_level(i, x, make_string, ...)
end

obj[("fmt_%s" ):format(x.name)] = function()
obj[("fmt_%s"):format(x.name)] = function()
return log_at_level(i, x, function(...)
local passed = {...}
local passed = { ... }
local fmt = table.remove(passed, 1)
local inspected = {}
for _, v in ipairs(passed) do
Expand Down
Loading