Add and use logger
#8
@@ -18,6 +18,7 @@ gleam_otp = ">= 1.2.0 and < 2.0.0"
|
|||||||
gleam_erlang = ">= 1.3.0 and < 2.0.0"
|
gleam_erlang = ">= 1.3.0 and < 2.0.0"
|
||||||
simplifile = ">= 2.3.1 and < 3.0.0"
|
simplifile = ">= 2.3.1 and < 3.0.0"
|
||||||
gleam_json = ">= 3.1.0 and < 4.0.0"
|
gleam_json = ">= 3.1.0 and < 4.0.0"
|
||||||
|
gleam_time = ">= 1.6.0 and < 2.0.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
gleeunit = ">= 1.0.0 and < 2.0.0"
|
gleeunit = ">= 1.0.0 and < 2.0.0"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ packages = [
|
|||||||
{ name = "gleam_json", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "44FDAA8847BE8FC48CA7A1C089706BD54BADCC4C45B237A992EDDF9F2CDB2836" },
|
{ name = "gleam_json", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "44FDAA8847BE8FC48CA7A1C089706BD54BADCC4C45B237A992EDDF9F2CDB2836" },
|
||||||
{ name = "gleam_otp", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "BA6A294E295E428EC1562DC1C11EA7530DCB981E8359134BEABC8493B7B2258E" },
|
{ name = "gleam_otp", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "BA6A294E295E428EC1562DC1C11EA7530DCB981E8359134BEABC8493B7B2258E" },
|
||||||
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
|
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
|
||||||
|
{ name = "gleam_time", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_time", source = "hex", outer_checksum = "0DF3834D20193F0A38D0EB21F0A78D48F2EC276C285969131B86DF8D4EF9E762" },
|
||||||
{ name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" },
|
{ name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" },
|
||||||
{ name = "simplifile", version = "2.3.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "957E0E5B75927659F1D2A1B7B75D7B9BA96FAA8D0C53EA71C4AD9CD0C6B848F6" },
|
{ name = "simplifile", version = "2.3.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "957E0E5B75927659F1D2A1B7B75D7B9BA96FAA8D0C53EA71C4AD9CD0C6B848F6" },
|
||||||
]
|
]
|
||||||
@@ -16,5 +17,6 @@ gleam_erlang = { version = ">= 1.3.0 and < 2.0.0" }
|
|||||||
gleam_json = { version = ">= 3.1.0 and < 4.0.0" }
|
gleam_json = { version = ">= 3.1.0 and < 4.0.0" }
|
||||||
gleam_otp = { version = ">= 1.2.0 and < 2.0.0" }
|
gleam_otp = { version = ">= 1.2.0 and < 2.0.0" }
|
||||||
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
|
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
|
||||||
|
gleam_time = { version = ">= 1.6.0 and < 2.0.0" }
|
||||||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
|
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
|
||||||
simplifile = { version = ">= 2.3.1 and < 3.0.0" }
|
simplifile = { version = ">= 2.3.1 and < 3.0.0" }
|
||||||
|
|||||||
@@ -2,17 +2,21 @@ import gleam/erlang/process.{type Name}
|
|||||||
|
|
||||||
import musicplayer/input/input
|
import musicplayer/input/input
|
||||||
import musicplayer/input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
|
import musicplayer/logging/logging
|
||||||
import musicplayer/mpv/mpv
|
import musicplayer/mpv/mpv
|
||||||
import musicplayer/musicplayer
|
import musicplayer/musicplayer
|
||||||
import musicplayer/ui/ui
|
import musicplayer/ui/ui
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
|
let assert Ok(logger) = logging.new("/tmp/musicplayer.log")
|
||||||
|
|
||||||
let input_keys_name: Name(Key) = process.new_name("input_keys")
|
let input_keys_name: Name(Key) = process.new_name("input_keys")
|
||||||
input.new(input_keys_name)
|
input.new(input_keys_name)
|
||||||
|
|
||||||
let assert Ok(ui) = ui.new()
|
let assert Ok(ui) = ui.new()
|
||||||
let assert Ok(mpv) = mpv.new()
|
let assert Ok(mpv) = mpv.new()
|
||||||
let assert Ok(musicplayer_pid) = musicplayer.new(ui, mpv, input_keys_name)
|
let assert Ok(musicplayer_pid) =
|
||||||
|
musicplayer.new(logger, ui, mpv, input_keys_name)
|
||||||
|
|
||||||
let monitor = process.monitor(musicplayer_pid)
|
let monitor = process.monitor(musicplayer_pid)
|
||||||
process.new_selector()
|
process.new_selector()
|
||||||
|
|||||||
7
src/musicplayer/logging/control.gleam
Normal file
7
src/musicplayer/logging/control.gleam
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import gleam/erlang/process.{type Subject}
|
||||||
|
|
||||||
|
pub type Control {
|
||||||
|
Write(String)
|
||||||
|
|
||||||
|
Exit(reply_to: Subject(Nil))
|
||||||
|
}
|
||||||
58
src/musicplayer/logging/logging.gleam
Normal file
58
src/musicplayer/logging/logging.gleam
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import gleam/erlang/process.{type Subject}
|
||||||
|
import gleam/otp/actor
|
||||||
|
import gleam/result
|
||||||
|
import gleam/string
|
||||||
|
import gleam/time/calendar
|
||||||
|
import gleam/time/timestamp
|
||||||
|
import simplifile
|
||||||
|
|
||||||
|
import musicplayer/logging/control.{type Control}
|
||||||
|
|
||||||
|
type State {
|
||||||
|
State(filepath: String)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(filepath: String) -> Result(Subject(Control), String) {
|
||||||
|
use _ <- result.try(
|
||||||
|
case simplifile.is_file(filepath) {
|
||||||
|
Ok(True) -> Ok(Nil)
|
||||||
|
_ -> simplifile.create_file(filepath)
|
||||||
|
}
|
||||||
|
|> result.map_error(fn(e) {
|
||||||
|
"Could not access or create log file: " <> string.inspect(e)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
actor.new(State(filepath:))
|
||||||
|
|> actor.on_message(handle_message)
|
||||||
|
|> actor.start
|
||||||
|
|> result.map_error(fn(start_error) {
|
||||||
|
"Could not start logger: " <> string.inspect(start_error)
|
||||||
|
})
|
||||||
|
|> result.map(fn(started) { started.data })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_message(state: State, control: Control) -> actor.Next(State, Control) {
|
||||||
|
case control {
|
||||||
|
control.Write(content) -> {
|
||||||
|
let log_line =
|
||||||
|
timestamp.system_time()
|
||||||
|
|> timestamp.to_rfc3339(calendar.utc_offset)
|
||||||
|
<> ": "
|
||||||
|
<> content
|
||||||
|
<> "\n"
|
||||||
|
|
||||||
|
// Ignore any logging errors
|
||||||
|
let _ = simplifile.append(state.filepath, log_line)
|
||||||
|
actor.continue(state)
|
||||||
|
}
|
||||||
|
control.Exit(reply_to) -> {
|
||||||
|
process.send(reply_to, Nil)
|
||||||
|
actor.stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log(logger: Subject(Control), content: String) -> Nil {
|
||||||
|
process.send(logger, control.Write(content))
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ pub fn new() -> Result(Subject(Control), String) {
|
|||||||
|> actor.start
|
|> actor.start
|
||||||
{
|
{
|
||||||
Error(start_error) ->
|
Error(start_error) ->
|
||||||
Error("Could not start actor: " <> string.inspect(start_error))
|
Error("Could not start mpv: " <> string.inspect(start_error))
|
||||||
Ok(actor.Started(data: mpv, ..)) -> Ok(mpv)
|
Ok(actor.Started(data: mpv, ..)) -> Ok(mpv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ import gleam/erlang/process.{type Name, type Pid, type Subject}
|
|||||||
import gleam/otp/actor
|
import gleam/otp/actor
|
||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
import musicplayer/time/time
|
|
||||||
|
|
||||||
import musicplayer/control.{type Control}
|
import musicplayer/control.{type Control}
|
||||||
import musicplayer/input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
|
import musicplayer/logging/control as logging_control
|
||||||
|
import musicplayer/logging/logging
|
||||||
import musicplayer/mpv/control as mpv_control
|
import musicplayer/mpv/control as mpv_control
|
||||||
|
import musicplayer/time/time
|
||||||
import musicplayer/ui/control as ui_control
|
import musicplayer/ui/control as ui_control
|
||||||
import musicplayer/ui/layout
|
import musicplayer/ui/layout
|
||||||
|
|
||||||
@@ -23,12 +25,14 @@ type State {
|
|||||||
State(
|
State(
|
||||||
mode: Mode,
|
mode: Mode,
|
||||||
input: Input,
|
input: Input,
|
||||||
|
logger: Subject(logging_control.Control),
|
||||||
ui: Subject(ui_control.Control),
|
ui: Subject(ui_control.Control),
|
||||||
mpv: Subject(mpv_control.Control),
|
mpv: Subject(mpv_control.Control),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
logger: Subject(logging_control.Control),
|
||||||
ui: Subject(ui_control.Control),
|
ui: Subject(ui_control.Control),
|
||||||
mpv: Subject(mpv_control.Control),
|
mpv: Subject(mpv_control.Control),
|
||||||
input_keys_name: Name(Key),
|
input_keys_name: Name(Key),
|
||||||
@@ -38,13 +42,14 @@ pub fn new(
|
|||||||
let input = Input(False, "")
|
let input = Input(False, "")
|
||||||
|
|
||||||
case
|
case
|
||||||
actor.new(State(Idle, input, ui, mpv))
|
actor.new(State(Idle, input, logger, ui, mpv))
|
||||||
|> actor.on_message(handle_message)
|
|> actor.on_message(handle_message)
|
||||||
|> actor.start
|
|> actor.start
|
||||||
{
|
{
|
||||||
Error(start_error) ->
|
Error(start_error) ->
|
||||||
Error("Could not start actor: " <> string.inspect(start_error))
|
Error("Could not start actor: " <> string.inspect(start_error))
|
||||||
Ok(actor.Started(pid:, data: musicplayer)) -> {
|
Ok(actor.Started(pid:, data: musicplayer)) -> {
|
||||||
|
logging.log(logger, "musicplayer - started")
|
||||||
process.spawn(fn() {
|
process.spawn(fn() {
|
||||||
let assert Ok(_) = process.register(process.self(), input_keys_name)
|
let assert Ok(_) = process.register(process.self(), input_keys_name)
|
||||||
handle_key(musicplayer, input_keys)
|
handle_key(musicplayer, input_keys)
|
||||||
@@ -60,6 +65,8 @@ pub fn new(
|
|||||||
fn handle_message(state: State, control: Control) -> actor.Next(State, Control) {
|
fn handle_message(state: State, control: Control) -> actor.Next(State, Control) {
|
||||||
case control {
|
case control {
|
||||||
control.Search -> {
|
control.Search -> {
|
||||||
|
logging.log(state.logger, "musicplayer - initiating search")
|
||||||
|
|
||||||
update_search(state.ui, "searching: ")
|
update_search(state.ui, "searching: ")
|
||||||
|
|
||||||
actor.continue(
|
actor.continue(
|
||||||
@@ -72,6 +79,8 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
}
|
}
|
||||||
|
|
||||||
control.Raw(content) -> {
|
control.Raw(content) -> {
|
||||||
|
logging.log(state.logger, "musicplayer - recieved raw input: " <> content)
|
||||||
|
|
||||||
let content = case state.mode {
|
let content = case state.mode {
|
||||||
Idle -> state.input.content
|
Idle -> state.input.content
|
||||||
Searching -> {
|
Searching -> {
|
||||||
@@ -84,6 +93,8 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
actor.continue(State(..state, input: Input(..state.input, content:)))
|
actor.continue(State(..state, input: Input(..state.input, content:)))
|
||||||
}
|
}
|
||||||
control.Backspace -> {
|
control.Backspace -> {
|
||||||
|
logging.log(state.logger, "musicplayer - recieved backspace")
|
||||||
|
|
||||||
let content = case state.mode {
|
let content = case state.mode {
|
||||||
Idle -> state.input.content
|
Idle -> state.input.content
|
||||||
Searching -> string.drop_end(state.input.content, 1)
|
Searching -> string.drop_end(state.input.content, 1)
|
||||||
@@ -91,6 +102,14 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
actor.continue(State(..state, input: Input(..state.input, content:)))
|
actor.continue(State(..state, input: Input(..state.input, content:)))
|
||||||
}
|
}
|
||||||
control.Return -> {
|
control.Return -> {
|
||||||
|
logging.log(
|
||||||
|
state.logger,
|
||||||
|
"musicplayer - recieved return. `input.capture`: "
|
||||||
|
<> "'"
|
||||||
|
<> state.input.content
|
||||||
|
<> "'",
|
||||||
|
)
|
||||||
|
|
||||||
// Note: state.input.content is now the final input, use it
|
// Note: state.input.content is now the final input, use it
|
||||||
// before it is reset
|
// before it is reset
|
||||||
case state.mode {
|
case state.mode {
|
||||||
@@ -104,17 +123,27 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
}
|
}
|
||||||
|
|
||||||
control.TogglePlayPause -> {
|
control.TogglePlayPause -> {
|
||||||
|
logging.log(state.logger, "musicplayer - toggling play/pause")
|
||||||
|
|
||||||
process.send(state.mpv, mpv_control.TogglePlayPause)
|
process.send(state.mpv, mpv_control.TogglePlayPause)
|
||||||
update_playback_time(state.mpv, state.ui)
|
update_playback_time(state.mpv, state.ui)
|
||||||
actor.continue(state)
|
actor.continue(state)
|
||||||
}
|
}
|
||||||
control.Exit -> {
|
control.Exit -> {
|
||||||
|
logging.log(state.logger, "musicplayer - initiating musicplayer shutdown")
|
||||||
// Close `mpv` socket
|
// Close `mpv` socket
|
||||||
process.call(state.mpv, 1000, fn(reply_to) { mpv_control.Exit(reply_to) })
|
process.call(state.mpv, 1000, fn(reply_to) { mpv_control.Exit(reply_to) })
|
||||||
|
|
||||||
// Reset terminal state (show cursor etc.)
|
// Reset terminal state (show cursor etc.)
|
||||||
process.call(state.ui, 1000, fn(reply_to) { ui_control.Exit(reply_to) })
|
process.call(state.ui, 1000, fn(reply_to) { ui_control.Exit(reply_to) })
|
||||||
|
|
||||||
|
logging.log(state.logger, "musicplayer - stopped")
|
||||||
|
|
||||||
|
// Close logger (NOOP)
|
||||||
|
process.call(state.logger, 1000, fn(reply_to) {
|
||||||
|
logging_control.Exit(reply_to)
|
||||||
|
})
|
||||||
|
|
||||||
actor.stop()
|
actor.stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user