Skip to content

Commit

Permalink
add even more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
t-aleksander committed Jun 13, 2024
1 parent 2e6f39d commit db70a7a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 33 deletions.
11 changes: 8 additions & 3 deletions src-tauri/src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl AppState {
info!("Removed connection from active connections: {removed_connection:#?}");
Some(removed_connection)
} else {
debug!("No active connection found with location_id: {location_id}");
None
}
}
Expand All @@ -97,11 +98,15 @@ impl AppState {
let active_connections = self.get_connections();
info!("Found {} active connections", active_connections.len());
for connection in active_connections {
debug!("Found active connection");
debug!(
"Found active connection with location {}",
connection.location_id
);
trace!("Connection: {connection:#?}");
debug!("Removing interface");
debug!("Removing interface {}", connection.interface_name);
disconnect_interface(connection, self).await?;
}
info!("All active connections closed");
Ok(())
}

Expand All @@ -123,7 +128,7 @@ impl AppState {
debug!("Found connection: {connection:#?}");
Some(connection.to_owned())
} else {
error!("Element with id: {id}, connection_type: {connection_type:?} not found.");
error!("Couldn't find connection with id: {id}, connection_type: {connection_type:?} in active connections.");
None
}
}
Expand Down
62 changes: 43 additions & 19 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub async fn disconnect(
info!("Disconnected from location with id: {location_id}");
Ok(())
} else {
error!("Connection for location with id: {location_id} not found");
error!("Error while disconnecting from location with id: {location_id} not found");
Err(Error::NotFound)
}
}
Expand Down Expand Up @@ -130,7 +130,7 @@ pub async fn save_device_config(
app_state: State<'_, AppState>,
handle: AppHandle,
) -> Result<SaveDeviceConfigResponse, Error> {
debug!("Received device configuration: {response:#?}");
debug!("Received device configuration: {response:#?}.");

let mut transaction = app_state.get_pool().begin().await?;
let instance_info = response
Expand Down Expand Up @@ -168,6 +168,7 @@ pub async fn save_device_config(
locations,
instance,
};
info!("Device configuration saved.");
Ok(res)
}

Expand Down Expand Up @@ -309,6 +310,7 @@ pub async fn update_instance(
let mut transaction = pool.begin().await?;

// update instance
debug!("Updating instance {instance_id}.");
let instance_info = response
.instance
.expect("Missing instance info in device config response");
Expand All @@ -319,6 +321,7 @@ pub async fn update_instance(
instance.save(&mut *transaction).await?;

// process locations received in response
debug!("Updating locations for instance {instance_id}.");
for location in response.configs {
// parse device config
let mut new_location = device_config_to_location(location, instance_id);
Expand All @@ -330,6 +333,10 @@ pub async fn update_instance(
{
// remove from list of existing locations
let mut current_location = current_locations.remove(position);
debug!(
"Updating existing location {} for instance {instance_id}.",
current_location.name
);
// update existing location
current_location.name = new_location.name;
current_location.address = new_location.address;
Expand All @@ -339,17 +346,26 @@ pub async fn update_instance(
current_location.mfa_enabled = new_location.mfa_enabled;
current_location.keepalive_interval = new_location.keepalive_interval;
current_location.save(&mut *transaction).await?;
info!(
"Location {} updated for instance {instance_id}.",
current_location.name
);
} else {
// create new location
debug!("Creating new location for instance {instance_id}.");
new_location.save(&mut *transaction).await?;
info!("New location created for instance {instance_id}.");
}
}
info!("Locations updated for instance {instance_id}.");

// remove locations which were present in current locations
// but no longer found in core response
debug!("Removing locations for instance {instance_id}.");
for removed_location in current_locations {
removed_location.delete(&mut *transaction).await?;
}
info!("Locations removed for instance {instance_id}.");

transaction.commit().await?;

Expand Down Expand Up @@ -456,7 +472,7 @@ pub async fn all_connections(
.collect()
}
};
debug!("Connections received, returning.");
info!("Connections retrieved({})", connections.len());
trace!("Connections found:\n{:#?}", connections);
Ok(connections)
}
Expand All @@ -469,7 +485,7 @@ pub async fn all_tunnel_connections(
debug!("Retrieving connections for location {location_id}");
let connections =
TunnelConnectionInfo::all_by_tunnel_id(&app_state.get_pool(), location_id).await?;
debug!("Connections received, returning.");
info!("Tunnel connections retrieved({})", connections.len());
trace!("Connections found:\n{:#?}", connections);
Ok(connections)
}
Expand All @@ -487,8 +503,8 @@ pub async fn active_connection(
if connection.is_some() {
debug!("Active connection found");
}
trace!("Connection:\n{:#?}", connection);
debug!("Connection returned");
trace!("Connection retrieved:\n{:#?}", connection);
info!("Connection retrieved");
Ok(connection)
}

Expand All @@ -503,18 +519,18 @@ pub async fn last_connection(
if let Some(connection) =
Connection::latest_by_location_id(&app_state.get_pool(), location_id).await?
{
trace!("Connection found");
info!("Found last connection at {}", connection.end);
Ok(Some(connection.into()))
} else {
Ok(None)
}
} else if let Some(connection) =
TunnelConnection::latest_by_tunnel_id(&app_state.get_pool(), location_id).await?
{
trace!("Connection found");
info!("Found last connection at {}", connection.end);
Ok(Some(connection.into()))
} else {
trace!("No last connection found");
info!("No last connection found");
Ok(None)
}
}
Expand All @@ -536,7 +552,7 @@ pub async fn update_location_routing(
{
location.route_all_traffic = route_all_traffic;
location.save(&app_state.get_pool()).await?;
debug!("Location routing updated for location {location_id}");
info!("Location routing updated for location {location_id}");
handle.emit_all(
"location-update",
Payload {
Expand All @@ -556,7 +572,7 @@ pub async fn update_location_routing(
{
tunnel.route_all_traffic = route_all_traffic;
tunnel.save(&app_state.get_pool()).await?;
debug!("Tunnel routing updated for tunnel {location_id}");
info!("Tunnel routing updated for tunnel {location_id}");
handle.emit_all(
"location-update",
Payload {
Expand All @@ -577,6 +593,7 @@ pub async fn get_settings(handle: AppHandle) -> Result<Settings, Error> {
debug!("Retrieving settings");
let app_state = handle.state::<AppState>();
let settings = Settings::get(&app_state.get_pool()).await?;
info!("Settings retrieved");
Ok(settings)
}

Expand All @@ -595,7 +612,7 @@ pub async fn update_settings(data: SettingsPatch, handle: AppHandle) -> Result<S
Ok(_) => {}
Err(e) => {
error!(
"During settings update, tray configuration update failed. err : {}",
"Tray configuration update failed during settings update. err : {}",
e.to_string()
);
}
Expand Down Expand Up @@ -632,7 +649,7 @@ pub async fn delete_instance(instance_id: i64, handle: AppHandle) -> Result<(),
error!("{msg}");
Error::InternalError(msg)
})?;
debug!("Connection closed and interface removed");
info!("Connection closed and interface removed");
}
}
}
Expand All @@ -648,10 +665,12 @@ pub async fn delete_instance(instance_id: i64, handle: AppHandle) -> Result<(),
#[tauri::command(async)]
pub async fn parse_tunnel_config(config: String) -> Result<Tunnel, Error> {
debug!("Parsing config file");
parse_wireguard_config(&config).map_err(|error| {
let tunnel_config = parse_wireguard_config(&config).map_err(|error| {
error!("{error}");
Error::ConfigParseError(error.to_string())
})
})?;
info!("Config file parsed");
Ok(tunnel_config)
}
#[tauri::command(async)]
pub async fn save_tunnel(mut tunnel: Tunnel, handle: AppHandle) -> Result<(), Error> {
Expand Down Expand Up @@ -700,6 +719,8 @@ pub async fn all_tunnels(app_state: State<'_, AppState>) -> Result<Vec<TunnelInf
connection_type: ConnectionType::Tunnel,
})
}

info!("Tunnels retrieved({})", tunnel_info.len());
Ok(tunnel_info)
}
#[tauri::command(async)]
Expand All @@ -710,7 +731,7 @@ pub async fn tunnel_details(
debug!("Retrieving Tunnel with ID {tunnel_id}.");

if let Some(tunnel) = Tunnel::find_by_id(&app_state.get_pool(), tunnel_id).await? {
debug!("Found tunnel {tunnel_id}");
info!("Found tunnel {tunnel_id}");
Ok(tunnel)
} else {
error!("Tunnel with ID: {tunnel_id}, not found");
Expand Down Expand Up @@ -745,7 +766,7 @@ pub async fn delete_tunnel(tunnel_id: i64, handle: AppHandle) -> Result<(), Erro
error!("{msg}");
Error::InternalError(msg)
})?;
debug!("Connection closed and interface removed");
info!("Connection closed and interface removed");
}
tunnel.delete(pool).await?;
} else {
Expand Down Expand Up @@ -798,10 +819,13 @@ pub async fn get_latest_app_version(handle: AppHandle) -> Result<AppVersionInfo,
let response_json: Result<AppVersionInfo, reqwest::Error> =
response.json::<AppVersionInfo>().await;

response_json.map_err(|err| {
let response = response_json.map_err(|err| {
error!("Failed to deserialize latest application version response {err}");
Error::CommandError(err.to_string())
})
})?;

info!("Latest application version fetched: {}", response.version);
Ok(response)
} else {
let err = res.err().unwrap();
error!("Failed to fetch latest application version {err}");
Expand Down
10 changes: 5 additions & 5 deletions src-tauri/src/service/log_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl ServiceLogWatcher {
fn parse_log_dir(&mut self) -> Result<(), LogWatcherError> {
// get latest log file
let latest_log_file = self.get_latest_log_file()?;
info!("found latest log file: {latest_log_file:?}");
debug!("found latest log file: {latest_log_file:?}");

// check if latest file changed
if latest_log_file.is_some() && latest_log_file != self.current_log_file {
Expand Down Expand Up @@ -180,9 +180,9 @@ impl ServiceLogWatcher {
/// Deserializes the log line into a known struct and checks if the line is relevant
/// to the specified interface. Also performs filtering by log level and optional timestamp.
fn parse_log_line(&self, line: String) -> Result<Option<LogLine>, LogWatcherError> {
debug!("Parsing log line: {line}");
trace!("Parsing log line: {line}");
let log_line = serde_json::from_str::<LogLine>(&line)?;
debug!("Parsed log line into: {log_line:?}");
trace!("Parsed log line into: {log_line:?}");

// filter by log level
if log_line.level > self.log_level {
Expand All @@ -205,7 +205,7 @@ impl ServiceLogWatcher {
if let Some(ref span) = log_line.span {
if let Some(interface_name) = &span.interface_name {
if interface_name != &self.interface_name {
debug!("Interface name {interface_name} is not the configured name {}. Skipping line...", self.interface_name);
trace!("Interface name {interface_name} is not the configured name {}. Skipping line...", self.interface_name);
return Ok(None);
}
}
Expand Down Expand Up @@ -244,7 +244,7 @@ impl ServiceLogWatcher {
}

fn extract_timestamp(filename: &str) -> Option<SystemTime> {
debug!("Extracting timestamp from log file name: {filename}");
trace!("Extracting timestamp from log file name: {filename}");
// we know that the date is always in the last 10 characters
let split_pos = filename.char_indices().nth_back(9)?.0;
let timestamp = &filename[split_pos..];
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn configure_tray_icon(app: &AppHandle, theme: &TrayIconTheme) -> Result<(),
Some(icon_path) => {
let icon = tauri::Icon::File(icon_path);
app.tray_handle().set_icon(icon)?;
debug!("Tray icon changed");
info!("Tray icon changed");
Ok(())
}
None => {
Expand Down
Loading

0 comments on commit db70a7a

Please sign in to comment.