From 0d6c61872e65d8a94f2c38a8c3a215b0f466d9e4 Mon Sep 17 00:00:00 2001 From: Masatoshi N Date: Sun, 30 Oct 2022 11:35:47 -0400 Subject: [PATCH] Add logger message count to MOTD --- lib/nerves_motd.ex | 13 +++++++++++++ lib/nerves_motd/runtime.ex | 15 +++++++++++++++ test/nerves_motd_test.exs | 7 +++++++ test/support/runtime.ex | 3 +++ 4 files changed, 38 insertions(+) diff --git a/lib/nerves_motd.ex b/lib/nerves_motd.ex index 3c43d72..260ee04 100644 --- a/lib/nerves_motd.ex +++ b/lib/nerves_motd.ex @@ -61,6 +61,8 @@ defmodule NervesMOTD do "\n", """ Nerves CLI help: https://hexdocs.pm/nerves/iex-with-nerves.html + + #{logger_text()} """ ] |> IO.ANSI.format() @@ -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 [ diff --git a/lib/nerves_motd/runtime.ex b/lib/nerves_motd/runtime.ex index f2920bc..defebaf 100644 --- a/lib/nerves_motd/runtime.ex +++ b/lib/nerves_motd/runtime.ex @@ -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()] @@ -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 diff --git a/test/nerves_motd_test.exs b/test/nerves_motd_test.exs index a2277f8..058956d 100644 --- a/test/nerves_motd_test.exs +++ b/test/nerves_motd_test.exs @@ -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()) diff --git a/test/support/runtime.ex b/test/support/runtime.ex index 1a7fdfe..8d23f66 100644 --- a/test/support/runtime.ex +++ b/test/support/runtime.ex @@ -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}