@@ -58,6 +58,7 @@ use serde_json::{json, Value};
5858use std:: borrow:: Cow ;
5959use std:: convert:: TryFrom ;
6060use std:: io:: Write ;
61+ use std:: path:: Path ;
6162use std:: str:: FromStr ;
6263use std:: sync:: Arc ;
6364
@@ -120,6 +121,7 @@ impl HeaderContext {
120121 Some ( HeaderComponent :: Cookie ) => self . add_cookie ( & data) . map ( PageContext :: Header ) ,
121122 Some ( HeaderComponent :: Authentication ) => self . authentication ( data) . await ,
122123 Some ( HeaderComponent :: Download ) => self . download ( & data) ,
124+ Some ( HeaderComponent :: Log ) => self . log ( & data) ,
123125 None => self . start_body ( data) . await ,
124126 }
125127 }
@@ -361,6 +363,11 @@ impl HeaderContext {
361363 ) )
362364 }
363365
366+ fn log ( self , data : & JsonValue ) -> anyhow:: Result < PageContext > {
367+ handle_log_component ( & self . request_context . source_path , Option :: None , data) ?;
368+ Ok ( PageContext :: Header ( self ) )
369+ }
370+
364371 async fn start_body ( self , data : JsonValue ) -> anyhow:: Result < PageContext > {
365372 let html_renderer =
366373 HtmlRenderContext :: new ( self . app_state , self . request_context , self . writer , data)
@@ -722,40 +729,17 @@ impl<W: std::io::Write> HtmlRenderContext<W> {
722729 component. starts_with ( PAGE_SHELL_COMPONENT )
723730 }
724731
725- fn handle_log_component ( & self , data : & JsonValue ) -> anyhow:: Result < ( ) > {
726- let mut log_level = log:: Level :: Info ;
727- if let Some ( priority) = get_object_str ( data, "priority" ) {
728- if let Ok ( level) = log:: Level :: from_str ( priority) {
729- log_level = level;
730- }
731- }
732-
733- let target = format ! (
734- "sqlpage::log from file \" {}\" in statement {}" ,
735- self . request_context. source_path. display( ) ,
736- self . current_statement
737- ) ;
738-
739- if let Some ( message) = get_object_str ( data, "message" ) {
740- log:: log!( target: & target, log_level, "{message}" ) ;
741- } else {
742- return Err ( anyhow:: anyhow!(
743- "message undefined for log in \" {}\" in statement {}" ,
744- self . request_context. source_path. display( ) ,
745- self . current_statement
746- ) ) ;
747- }
748-
749- Ok ( ( ) )
750- }
751-
752732 async fn handle_component ( & mut self , comp_str : & str , data : & JsonValue ) -> anyhow:: Result < ( ) > {
753733 if Self :: is_shell_component ( comp_str) {
754734 bail ! ( "There cannot be more than a single shell per page. You are trying to open the {} component, but a shell component is already opened for the current page. You can fix this by removing the extra shell component, or by moving this component to the top of the SQL file, before any other component that displays data." , comp_str) ;
755735 }
756736
757737 if comp_str == "log" {
758- return self . handle_log_component ( data) ;
738+ return handle_log_component (
739+ & self . request_context . source_path ,
740+ Some ( self . current_statement ) ,
741+ data,
742+ ) ;
759743 }
760744
761745 match self . open_component_with_data ( comp_str, & data) . await {
@@ -921,6 +905,43 @@ impl<W: std::io::Write> HtmlRenderContext<W> {
921905 }
922906}
923907
908+ fn handle_log_component (
909+ source_path : & Path ,
910+ current_statement : Option < usize > ,
911+ data : & JsonValue ,
912+ ) -> anyhow:: Result < ( ) > {
913+ let mut log_level = log:: Level :: Info ;
914+ if let Some ( priority) = get_object_str ( data, "priority" ) {
915+ if let Ok ( level) = log:: Level :: from_str ( priority) {
916+ log_level = level;
917+ }
918+ }
919+
920+ let current_statement_string = if let Some ( option) = current_statement {
921+ & format ! ( "statement {option}" )
922+ } else {
923+ "header"
924+ } ;
925+
926+ let target = format ! (
927+ "sqlpage::log from file \" {}\" in {}" ,
928+ source_path. display( ) ,
929+ current_statement_string
930+ ) ;
931+
932+ if let Some ( message) = get_object_str ( data, "message" ) {
933+ log:: log!( target: & target, log_level, "{message}" ) ;
934+ } else {
935+ return Err ( anyhow:: anyhow!(
936+ "message undefined for log in \" {}\" in {}" ,
937+ source_path. display( ) ,
938+ current_statement_string
939+ ) ) ;
940+ }
941+
942+ Ok ( ( ) )
943+ }
944+
924945pub ( super ) fn get_backtrace_as_strings ( error : & anyhow:: Error ) -> Vec < String > {
925946 let mut backtrace = vec ! [ ] ;
926947 let mut source = error. source ( ) ;
@@ -1144,6 +1165,7 @@ enum HeaderComponent {
11441165 Cookie ,
11451166 Authentication ,
11461167 Download ,
1168+ Log ,
11471169}
11481170
11491171impl TryFrom < & str > for HeaderComponent {
@@ -1158,6 +1180,7 @@ impl TryFrom<&str> for HeaderComponent {
11581180 "cookie" => Ok ( Self :: Cookie ) ,
11591181 "authentication" => Ok ( Self :: Authentication ) ,
11601182 "download" => Ok ( Self :: Download ) ,
1183+ "log" => Ok ( Self :: Log ) ,
11611184 _ => Err ( ( ) ) ,
11621185 }
11631186 }
0 commit comments