Skip to content
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

Add logger message count to MOTD #98

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions lib/nerves_motd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ defmodule NervesMOTD do
"\n",
"""
Nerves CLI help: https://hexdocs.pm/nerves/iex-with-nerves.html

#{logger_text()}
"""
]
|> IO.ANSI.format()
Expand All @@ -79,6 +81,17 @@ defmodule NervesMOTD do
Keyword.get(opts, :logo, @logo)
end

@spec logger_text() :: String.t()
defp logger_text() do
case runtime_mod().count_log_messages() do
{:ok, count} ->
"You have #{count} unread log messages"

_ ->
""
end
end

@spec rows(map(), list()) :: [[cell()]]
defp rows(apps, opts) do
[
Expand Down
15 changes: 15 additions & 0 deletions lib/nerves_motd/runtime.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule NervesMOTD.Runtime do
@moduledoc false
@callback applications() :: %{started: [atom()], loaded: [atom()]}
@callback count_log_messages() :: {:ok, non_neg_integer()} | :error
@callback cpu_temperature() :: {:ok, float()} | :error
@callback firmware_valid?() :: boolean()
@callback load_average() :: [String.t()]
Expand Down Expand Up @@ -34,6 +35,20 @@ defmodule NervesMOTD.Runtime.Target do
%{started: started, loaded: loaded}
end

if Code.ensure_loaded?(RingLogger) and
not is_nil(apply(RingLogger, :__info__, [:functions])[:count_next]) do
@impl NervesMOTD.Runtime
def count_log_messages() do
case apply(RingLogger, :count_next, []) do
count when is_integer(count) -> {:ok, count}
_ -> :error
end
end
else
@impl NervesMOTD.Runtime
def count_log_messages(), do: :error
end

@impl NervesMOTD.Runtime
def cpu_temperature() do
# Read the file /sys/class/thermal/thermal_zone0/temp. The file content is
Expand Down
7 changes: 7 additions & 0 deletions test/nerves_motd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ defmodule NervesMOTDTest do
refute capture_motd(logo: "") =~ @nerves_logo_regex
end

test "Logger text" do
NervesMOTD.MockRuntime
|> Mox.expect(:applications, 1, default_applications_code())

assert capture_motd() =~ ~r/You have 123 unread log messages/
end

test "Custom rows" do
NervesMOTD.MockRuntime
|> Mox.expect(:applications, 1, default_applications_code())
Expand Down
3 changes: 3 additions & 0 deletions test/support/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ defmodule NervesMOTD.Runtime.Host do
@impl NervesMOTD.Runtime
def applications(), do: %{loaded: @apps, started: @apps}

@impl NervesMOTD.Runtime
def count_log_messages(), do: {:ok, 123}

@impl NervesMOTD.Runtime
def cpu_temperature(), do: {:ok, 41.234}

Expand Down