Skip to content

Commit e4a584a

Browse files
committed
Formatting
1 parent 2b32e50 commit e4a584a

4 files changed

Lines changed: 54 additions & 25 deletions

File tree

bundlex.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ defmodule Unifex.BundlexProject do
2222
],
2323
unifex: [
2424
src_base: "unifex/cnode/unifex",
25-
sources: ["unifex.c", "cnode.c", "payload.c", "../../unifex/logger.c", "../../unifex/logger_cnode.c"],
25+
sources: [
26+
"unifex.c",
27+
"cnode.c",
28+
"payload.c",
29+
"../../unifex/logger.c",
30+
"../../unifex/logger_cnode.c"
31+
],
2632
includes: [Path.join(__DIR__, "c_src/unifex")],
2733
libs: ["pthread"],
2834
interface: :cnode

lib/unifex/logger.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ defmodule Unifex.Logger do
3030
def handle_info({:unifex_logger, level, message, timestamp, tags}, state) do
3131
metadata = [tags: tags, unifex_nif: true, timestamp: timestamp]
3232

33-
Logger.log(normalize_level(level), fn -> format_message(message, tags, timestamp) end, metadata)
33+
Logger.log(
34+
normalize_level(level),
35+
fn -> format_message(message, tags, timestamp) end,
36+
metadata
37+
)
3438

3539
{:noreply, state}
3640
end

test/unifex/logger_integration_test.exs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ defmodule Unifex.LoggerIntegrationTest do
88
unless Process.whereis(Unifex.Logger) do
99
{:ok, _pid} = Unifex.Logger.start_link([])
1010
end
11+
1112
:ok
1213
end
1314

1415
describe "Unifex.Logger integration with native code" do
15-
1616
@tag :integration
1717
test "logger process handles messages from native code" do
1818
# This test would require a native function that calls unifex_log()
1919
# For now, we'll test that the logger process can handle direct messages
2020
pid = Process.whereis(Unifex.Logger)
21-
21+
2222
# Send a message directly to the logger
23-
send(pid, {:unifex_logger, :info, "Test message", 1700000000000000, ["test", "direct"]})
24-
23+
send(
24+
pid,
25+
{:unifex_logger, :info, "Test message", 1_700_000_000_000_000, ["test", "direct"]}
26+
)
27+
2528
# The logger should process it without crashing
2629
assert Process.alive?(pid)
2730
end
@@ -30,7 +33,7 @@ defmodule Unifex.LoggerIntegrationTest do
3033
test "logger normalizes all valid Elixir log levels" do
3134
for level <- @valid_levels do
3235
pid = Process.whereis(Unifex.Logger)
33-
send(pid, {:unifex_logger, level, "Test message", 1700000000000000, ["test"]})
36+
send(pid, {:unifex_logger, level, "Test message", 1_700_000_000_000_000, ["test"]})
3437
assert Process.alive?(pid)
3538
end
3639
end
@@ -39,36 +42,41 @@ defmodule Unifex.LoggerIntegrationTest do
3942
test "logger handles string level from C code" do
4043
# The C code uses string levels like "debug", "info", "warning", "error"
4144
pid = Process.whereis(Unifex.Logger)
42-
45+
4346
# Test each of the UNIFEX_LOG_LEVEL_ constants
4447
for level <- ["debug", "info", "warning", "error"] do
45-
send(pid, {:unifex_logger, level, "Test message", 1700000000000000, ["test"]})
48+
send(pid, {:unifex_logger, level, "Test message", 1_700_000_000_000_000, ["test"]})
4649
assert Process.alive?(pid)
4750
end
4851
end
4952

5053
@tag :integration
5154
test "logger handles unknown levels gracefully" do
5255
pid = Process.whereis(Unifex.Logger)
53-
56+
5457
# Send a message with an unknown level
55-
send(pid, {:unifex_logger, :unknown_level, "Test message", 1700000000000000, ["test"]})
56-
58+
send(pid, {:unifex_logger, :unknown_level, "Test message", 1_700_000_000_000_000, ["test"]})
59+
5760
# Should still be alive and should have logged a warning about unknown level
5861
assert Process.alive?(pid)
5962
end
6063

6164
@tag :integration
6265
test "logger handles messages with empty tags" do
6366
pid = Process.whereis(Unifex.Logger)
64-
send(pid, {:unifex_logger, :info, "Test message", 1700000000000000, []})
67+
send(pid, {:unifex_logger, :info, "Test message", 1_700_000_000_000_000, []})
6568
assert Process.alive?(pid)
6669
end
6770

6871
@tag :integration
6972
test "logger handles messages with nil tags" do
7073
pid = Process.whereis(Unifex.Logger)
71-
send(pid, {:unifex_logger, :info, "Test message", 1700000000000000, [nil, "valid", nil]})
74+
75+
send(
76+
pid,
77+
{:unifex_logger, :info, "Test message", 1_700_000_000_000_000, [nil, "valid", nil]}
78+
)
79+
7280
assert Process.alive?(pid)
7381
end
7482
end
@@ -83,4 +91,4 @@ defmodule Unifex.LoggerIntegrationTest do
8391
assert Process.alive?(pid)
8492
end
8593
end
86-
end
94+
end

test/unifex/logger_test.exs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,21 @@ defmodule Unifex.LoggerTest do
3535
end
3636

3737
test "formats message without tags" do
38-
timestamp = 1700000000000000 # microseconds since epoch
38+
# microseconds since epoch
39+
timestamp = 1_700_000_000_000_000
3940
message = "Test message"
4041
tags = []
4142

4243
formatted = Unifex.Logger.format_message(message, tags, timestamp)
4344

4445
# Should contain the message and timestamp
4546
assert String.contains?(formatted, message)
46-
assert String.contains?(formatted, "2023-11-14") # Approximate date for timestamp
47+
# Approximate date for timestamp
48+
assert String.contains?(formatted, "2023-11-14")
4749
end
4850

4951
test "formats message with tags" do
50-
timestamp = 1700000000000000
52+
timestamp = 1_700_000_000_000_000
5153
message = "Test message"
5254
tags = ["tag1", "tag2"]
5355

@@ -60,19 +62,21 @@ defmodule Unifex.LoggerTest do
6062

6163
test "formats timestamp correctly" do
6264
# Test with a known timestamp
63-
timestamp = 1700000000000000 # 2023-11-14T22:13:20.000000Z
65+
# 2023-11-14T22:13:20.000000Z
66+
timestamp = 1_700_000_000_000_000
6467
formatted_timestamp = Unifex.Logger.format_timestamp(timestamp)
6568
assert formatted_timestamp == "2023-11-14T22:13:20.000000Z"
6669
end
6770

6871
test "handles nil tags gracefully" do
69-
timestamp = 1700000000000000
72+
timestamp = 1_700_000_000_000_000
7073
message = "Test message"
7174
tags = [nil, "valid_tag", nil]
7275

7376
formatted = Unifex.Logger.format_message(message, tags, timestamp)
7477

75-
assert String.contains?(formatted, "[]") # nil tags become empty brackets
78+
# nil tags become empty brackets
79+
assert String.contains?(formatted, "[]")
7680
assert String.contains?(formatted, "[valid_tag]")
7781
end
7882

@@ -84,7 +88,7 @@ defmodule Unifex.LoggerTest do
8488
# Send a log message directly to the logger process
8589
level = :info
8690
message = "Test integration message"
87-
timestamp = 1700000000000000
91+
timestamp = 1_700_000_000_000_000
8892
tags = ["integration", "test"]
8993

9094
send(pid, {:unifex_logger, level, message, timestamp, tags})
@@ -96,8 +100,14 @@ defmodule Unifex.LoggerTest do
96100
end
97101
else
98102
{:ok, pid} = Unifex.Logger.start_link([])
103+
99104
try do
100-
send(pid, {:unifex_logger, :info, "Test integration message", 1700000000000000, ["integration", "test"]})
105+
send(
106+
pid,
107+
{:unifex_logger, :info, "Test integration message", 1_700_000_000_000_000,
108+
["integration", "test"]}
109+
)
110+
101111
assert Process.alive?(pid)
102112
after
103113
GenServer.stop(pid)
@@ -117,6 +127,7 @@ defmodule Unifex.LoggerTest do
117127
end
118128
else
119129
{:ok, pid} = Unifex.Logger.start_link([])
130+
120131
try do
121132
send(pid, {:unknown, :message})
122133
assert Process.alive?(pid)
@@ -134,7 +145,7 @@ defmodule Unifex.LoggerTest do
134145
# #define UNIFEX_LOG_LEVEL_INFO "info"
135146
# #define UNIFEX_LOG_LEVEL_WARN "warning"
136147
# #define UNIFEX_LOG_LEVEL_ERROR "error"
137-
148+
138149
# We verify that these atom values are valid Elixir logger levels
139150
# (note: the C code sends string levels which get normalized to atoms)
140151
assert :debug in @valid_levels
@@ -143,4 +154,4 @@ defmodule Unifex.LoggerTest do
143154
assert :error in @valid_levels
144155
end
145156
end
146-
end
157+
end

0 commit comments

Comments
 (0)