From 025b5d1dcbb30e727afee3307d49328432bae603 Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Fri, 9 Apr 2021 08:09:20 -0400 Subject: Modified how logging works so that multiple processes can write in theory. --- captain/api.f90 | 18 ++++++++---------- captain/captian.f90 | 7 ++++--- captain/config.f90 | 6 ++++++ captain/db.f90 | 11 +---------- captain/example/levitating.conf | 2 ++ captain/external.f90 | 28 +++++++++++---------------- captain/gemini.f90 | 42 ++++++++++++++++++++--------------------- captain/log.f90 | 9 ++++++++- captain/response.f90 | 12 ++++++------ common/protocol.f90 | 2 +- common/request.f90 | 2 +- 11 files changed, 69 insertions(+), 70 deletions(-) diff --git a/captain/api.f90 b/captain/api.f90 index b330d30..fc4e248 100644 --- a/captain/api.f90 +++ b/captain/api.f90 @@ -41,9 +41,9 @@ contains if(associated(req%query_string)) then job_i = req%path_component_int(5) task_i = req%path_component_int(7) - call write_log("task update is "//trim(req%query_string)) + call write_log("Task Update Is "//trim(req%query_string), LOG_DEBUG) if(req%query_string == "starting") then - call write_log("Inserting task") + call write_log("Inserting task", LOG_DEBUG) call insert_task(job_i, task_i) call update_job_status(job_i, JOB_STATUS_WORKING) else if(req%query_string == "inprogress") then @@ -78,13 +78,11 @@ contains ! Task - "/api/player/{name}/job/{jobid}/task/{task num}" if(trim(req%component(2)) == "player" .and. trim(req%component(4)) == "job") then job_i = req%path_component_int(5) - call write_log("Job "//trim(req%component(5))//" update arrived") + call write_log("Job "//trim(req%component(5))//" update arrived", LOG_INFO) write(player, *) job_i - call write_log("int is "//trim(player)) if(.not. is_final_job_status(job_i)) then - call write_log("Processing line") if(trim(req%component(6)) == "complete") then call update_job_status(job_i, JOB_STATUS_SUCCESS) else if(trim(req%component(6)) == "failure") then @@ -93,7 +91,7 @@ contains end if if(trim(req%component(6)) == "task") then - call write_log("Task update encountered") + call write_log("Task update encountered", LOG_INFO) call handle_task_request(req) end if @@ -119,7 +117,7 @@ contains checkin_work_json => build_job_available_json(job_i) if(associated(checkin_work_json)) then resp%code = GEMINI_CODE_SUCCESS - call write_log("Sending: "//trim(checkin_work_json)) + call write_log("Sending: "//trim(checkin_work_json), LOG_DEBUG) call resp%set_body_contents(trim(checkin_work_json), "text/gemini") deallocate(checkin_work_json) else @@ -161,7 +159,7 @@ contains fullpath => null() - call write_log("Titan request encountered") + call write_log("Titan request encountered", LOG_INFO) ! Task - "/api/player/{name}/job/{jobid}/task/{task num}" if(trim(req%component(2)) == "player" .and. & @@ -175,7 +173,7 @@ contains write(job_text, '(I6)') job_id write(task_text, '(I6)') task_num - call write_log("Handling a task update for job "//trim(job_text)//" task "//trim(task_text)) + call write_log("Handling a task update for job "//trim(job_text)//" task "//trim(task_text), LOG_INFO) call handle_task_request(req) fullpath => get_task_result_static_filename(job_id, task_num) @@ -185,7 +183,7 @@ contains if(associated(fullpath)) then ! Write the file - call write_log("Storing titan file to "//trim(fullpath)) + call write_log("Storing titan file to "//trim(fullpath), LOG_DEBUG) call req%write_to(fullpath) diff --git a/captain/captian.f90 b/captain/captian.f90 index eb278a6..f5ee308 100644 --- a/captain/captian.f90 +++ b/captain/captian.f90 @@ -1,7 +1,7 @@ program captain use captain_db use config -use logging, only: initialize_log => initialize, logunit +use logging, only: initialize_log => initialize, shutdown_log => shutdown use gemini, only: handle_gemini => handle_request implicit none @@ -13,7 +13,7 @@ implicit none call parse_options() - call initialize_log(log_filename) + call initialize_log(log_filename, loglevel) call initialize_db(database_filename) @@ -23,7 +23,7 @@ implicit none end select call shutdown_db() - close(logunit) + call shutdown_log() contains @@ -55,6 +55,7 @@ contains config_loaded = .false. mode = MODE_CGI_HTML + loglevel = 3 i = 1 do while(i <= command_argument_count()) diff --git a/captain/config.f90 b/captain/config.f90 index 6c622b4..5b13584 100644 --- a/captain/config.f90 +++ b/captain/config.f90 @@ -10,6 +10,9 @@ implicit none character(*), parameter::LOGFILE_VARIABLE = "log-filename" character(1024)::log_filename + character(*), parameter::LOGLEVEL_VARIABLE = "log-level" + integer::loglevel = 3 + character(*), parameter::PROJECT_NAME_VARIABLE = "project" character(32)::project @@ -121,6 +124,9 @@ contains else if(cvariable == INSTRUCTIONS_DIRECTORY_VARIABLE) then instructions_dir = cvalue + + else if(cvariable == LOGLEVEL_VARIABLE) then + read(cvalue, '(I3)') loglevel end if diff --git a/captain/db.f90 b/captain/db.f90 index 56e8a70..e3a9f50 100644 --- a/captain/db.f90 +++ b/captain/db.f90 @@ -569,16 +569,11 @@ contains end function get_job_tasks subroutine update_job_status(job_id, status) - use logging implicit none integer, intent(in)::job_id, status type(sqlite3_stmt)::stmt - character(128)::msg - - write(msg, *) "Update ", job_id, " to ", status - call write_log(trim(msg)) - + if(stmt%prepare(db, "UPDATE jobs SET status=? WHERE id=?") == SQLITE_OK) then if(stmt%bind_int(1, status) == SQLITE_OK .and. stmt%bind_int(2, job_id) == SQLITE_OK) then call stmt%step_now() @@ -652,19 +647,15 @@ contains end subroutine update_task_status function is_final_job_status(job_id) - use logging implicit none integer, intent(in)::job_id logical::is_final_job_status integer::i - character(128)::s i = get_job_status(job_id) is_final_job_status = (i == JOB_STATUS_SUCCESS .or. i == JOB_STATUS_FAILURE) - write(s, *) "Status for ", job_id, " is ", i, " or ", is_final_job_status - call write_log(trim(s)) end function is_final_job_status diff --git a/captain/example/levitating.conf b/captain/example/levitating.conf index 1b647a4..5726e9b 100644 --- a/captain/example/levitating.conf +++ b/captain/example/levitating.conf @@ -5,6 +5,8 @@ database = /home/jeff/Workspace/levitating/captain/example/store.db log-filename = /tmp/levitating.log +log-level = 4 + project = misc-build description = A builder for stuff diff --git a/captain/external.f90 b/captain/external.f90 index 5cbee17..518abcb 100644 --- a/captain/external.f90 +++ b/captain/external.f90 @@ -119,7 +119,6 @@ contains integer::i_start_jobs, ierr character(len=:), pointer::linklist - character(64)::dbgstr n = get_jobs_count() if(n == 0) then @@ -147,9 +146,6 @@ contains res = "## Jobs " - write(dbgstr, '(I3,1X,I3)') i_start_jobs, min(i_start_jobs+14, n) - call write_log("Jobs between "//trim(dbgstr)) - linklist => render_jobs_links(jobs, i_start_jobs, min(i_start_jobs+14, n)) res = trim(res)//trim(linklist) call write_log(linklist) @@ -586,7 +582,7 @@ contains ext = " " end if - call write_log("Static check: "//trim(first)) + call write_log("Static check: "//trim(first), LOG_DEBUG) is_request_static = ((trim(first) == "releases") .or. & (trim(first) == "uploads") .or. & @@ -630,7 +626,7 @@ contains end function external_redirect_action_request_gemini function external_request_static(req) result(resp) - use logging, only: write_log + use logging use config use utilities use server_response @@ -653,7 +649,7 @@ contains inquire(file=resp%body_filename, exist=exists) if(.not. exists) then resp%code = GEMINI_CODE_PERMFAIL - call write_log("File did not exist: "//resp%body_filename) + call write_log("File did not exist: "//resp%body_filename, LOG_NORMAL) else resp%code = GEMINI_CODE_SUCCESS @@ -678,7 +674,7 @@ contains function external_request_templated(req) result(resp) use page_template use config, only: template_filepath, project - use logging, only: write_log + use logging use server_response implicit none @@ -694,15 +690,13 @@ contains call template_filepath("index.gmi", template_file) call page%init(trim(template_file)) - call write_log("Processing request") + call write_log("Processing request", LOG_INFO) call req%path_component(1, first) if(trim(req%location) == "/" .or. trim(req%location) == "/index.gmi") then - call write_log("Assign") call page%assign('title', 'Home') - call write_log("Assign done") else if(trim(req%location) == "/releases.gmi") then @@ -757,7 +751,7 @@ contains call write_log("Rendering page for "//req%location) call page%render() - call write_log("Finalizing response") + call write_log("Finalizing response", LOG_INFO) resp%temporary_file = .true. resp%body_filename => page%output_filename resp%body_mimetype = "text/gemini" @@ -774,23 +768,23 @@ contains type(response)::resp if(is_redirect_action(req)) then - call write_log("Action request") + call write_log("Action request", LOG_INFO) resp = external_redirect_action_request_gemini(req) else if(is_input_provided_request(req)) then - call write_log("Input request") + call write_log("Input request", LOG_INFO) resp = external_input_request_gemini(req) else if(is_input_required_request(req)) then - call write_log("Input required") + call write_log("Input required", LOG_INFO) resp = external_input_required_gemini(req) else if(is_request_static(req)) then - call write_log("Req static") + call write_log("Req static", LOG_INFO) resp = external_request_static(req) else - call write_log("Req template") + call write_log("Req template", LOG_INFO) resp = external_request_templated(req) end if diff --git a/captain/gemini.f90 b/captain/gemini.f90 index 02b57c7..099a81e 100644 --- a/captain/gemini.f90 +++ b/captain/gemini.f90 @@ -86,7 +86,7 @@ contains write(int_text, '(I8)') code line = trim(adjustl(int_text))//" "//trim(meta) - call write_log("Status line: '"//trim(line)//"'") + call write_log("Status line: '"//trim(line)//"'", LOG_DEBUG) call write_string(ssl, trim(line)//c_carriage_return//c_new_line) @@ -179,7 +179,7 @@ contains use iso_fortran_env use external_handling, only: external_request_gemini use api_handling - use logging, only: write_log + use logging use server_response implicit none @@ -206,7 +206,7 @@ contains ctx = ctx_new(method) if(.not. C_ASSOCIATED(ctx)) then - call write_log("Context failed") + call write_log("Context failed", LOG_NORMAL) return end if @@ -214,61 +214,61 @@ contains !res = ctx_set_ecdh_auto(ctx, 1) if(.not. ctx_use_certificate_file(ctx, trim(pubcert), SSL_FILETYPE_PEM)) then - call write_log("Cert file failed") - call write_log("Public: "//trim(pubcert)) + call write_log("Cert file failed", LOG_NORMAL) + call write_log("Public: "//trim(pubcert), LOG_NORMAL) !call print_error() return end if if(.not. ctx_use_private_key_file(ctx, trim(privcert), SSL_FILETYPE_PEM)) then - call write_log("Cert file failed") - call write_log("Private: "//trim(privcert)) + call write_log("Cert file failed", LOG_NORMAL) + call write_log("Private: "//trim(privcert), LOG_NORMAL) !call print_error() return end if ssl = ssl_new(ctx) - call write_log("Initiating connection") + call write_log("Initiating connection", LOG_DEBUG) ! So this is a GNU Extension... res = set_read_fd(ssl, fnum(input_unit)) if(res /= 1) then - call write_log("set rfd failed") + call write_log("set rfd failed", LOG_NORMAL) !call print_error() return end if res = set_write_fd(ssl, fnum(output_unit)) if(res /= 1) then - call write_log("set wfd failed") + call write_log("set wfd failed", LOG_NORMAL) !call print_error() return end if res = ssl_accept(ssl) if(res <= 0) then - call write_log("ssl_accept failed") + call write_log("ssl_accept failed", LOG_NORMAL) !call print_error() return end if - call write_log("Handling read_request") + call write_log("Handling read_request", LOG_DEBUG) ! Do the actual protocol nonsense call read_request(ssl, text_request) - call write_log("Initializing object") + call write_log("Initializing object", LOG_DEBUG) call req%init(text_request) - call write_log("Request object created") + call write_log("Request object created", LOG_DEBUG) call req%path_component(1, first) if(trim(first) == 'api') then - call write_log("API call encountered") + call write_log("API call encountered", LOG_DEBUG) if(req%protocol == "gemini") then resp = api_request_gemini(req) - call write_log("resp filename is: '"//trim(resp%body_filename)//"'") + call write_log("resp filename is: '"//trim(resp%body_filename)//"'", LOG_DEBUG) else if(req%protocol == "titan") then call treq%init_from_request(req, ssl) resp = api_request_titan(treq) @@ -277,7 +277,7 @@ contains resp = external_request_gemini(req) end if - call write_log("Handling response") + call write_log("Handling response", LOG_DEBUG) ! Handle the response select case(resp%code) case(GEMINI_CODE_INPUT) @@ -290,21 +290,21 @@ contains call write_failure(ssl) case(GEMINI_CODE_SUCCESS) - call write_log("Sending '"//trim(resp%body_filename)//"' as "//trim(resp%body_mimetype)) + call write_log("Sending '"//trim(resp%body_filename)//"' as "//trim(resp%body_mimetype), LOG_DEBUG) open(newunit=rendered_unit, file=resp%body_filename, status="old", & form="unformatted", iostat=ioerror, access="stream") call write_file(ssl, rendered_unit, resp%body_mimetype) - call write_log("File written") + call write_log("File written", LOG_DEBUG) close(rendered_unit) end select - call write_log("Cleanup") + call write_log("Cleanup", LOG_DEBUG) call req%destroy() call resp%destroy() - call write_log("shutdown") + call write_log("Shutdown", LOG_DEBUG) res = ssl_shutdown(ssl) res = ssl_free(ssl) res = ctx_free(ctx) diff --git a/captain/log.f90 b/captain/log.f90 index 44ba46e..4289f80 100644 --- a/captain/log.f90 +++ b/captain/log.f90 @@ -1,6 +1,10 @@ module logging implicit none + integer, parameter::LOG_DEBUG = 8 + integer, parameter::LOG_INFO = 6 + integer, parameter::LOG_NORMAL = 3 + integer, parameter::logunit = 1197 integer::level_threshold @@ -36,6 +40,7 @@ contains character(*), intent(in)::string integer, intent(in), optional::level + character(12)::pid_text integer::ierr if(present(level)) then @@ -50,8 +55,10 @@ contains ! Silently move on if we failed to open the file if(ierr == 0) then + write(pid_text, '(I8)') getpid() + ! GNU Extension... :( - write(logunit, *) fdate()//" :: "//string + write(logunit, *) fdate()//" ("//trim(adjustl(pid_text))//") :: "//string call close(logunit) end if diff --git a/captain/response.f90 b/captain/response.f90 index b980f8e..efa99bb 100644 --- a/captain/response.f90 +++ b/captain/response.f90 @@ -80,12 +80,12 @@ contains n = len_trim(str) allocate(character(len=n) :: self%url) self%url = trim(str) - call write_log("URL: "//self%url) + call write_log("URL: "//self%url, LOG_NORMAL) i = index(str, "://") allocate(character(len=(i-1)) :: self%protocol) self%protocol = str(1:i-1) - call write_log("Protocol: "//self%protocol) + call write_log("Protocol: "//self%protocol, LOG_DEBUG) i = i + 3 j = index(str(i:n), "/") @@ -94,7 +94,7 @@ contains end if allocate(character(len=(j-1)) :: self%server) self%server = str(i:(i+j-1)) - call write_log("Server: "//self%server//"|") + call write_log("Server: "//self%server//"|", LOG_DEBUG) i = j+i-1 j = index(str, "?") @@ -106,16 +106,16 @@ contains allocate(character(len=(n - i + 1)) :: self%location) self%location = str(i:n) end if - call write_log("Location: "//self%location) + call write_log("Location: "//self%location, LOG_DEBUG) else allocate(character(len=(n-j)) :: self%query_string) self%query_string = str(j+1:n) - call write_log("Query: "//self%query_string) + call write_log("Query: "//self%query_string, LOG_DEBUG) allocate(character(len=(j-i)) :: self%location) self%location = str(i:j-1) - call write_log("Location: "//self%location) + call write_log("Location: "//self%location, LOG_DEBUG) end if end subroutine request_init diff --git a/common/protocol.f90 b/common/protocol.f90 index 37e3205..992283c 100644 --- a/common/protocol.f90 +++ b/common/protocol.f90 @@ -228,7 +228,7 @@ contains character, dimension(BUFFER_SIZE)::buffer logical, dimension(4)::successes - integer::i, ierr, bytes_read, bytes_written, total_written + integer::i, bytes_read, bytes_written, total_written ! For direct processing of the reponse line integer::response_line_index, bytes_received diff --git a/common/request.f90 b/common/request.f90 index e9043c0..d7b3120 100644 --- a/common/request.f90 +++ b/common/request.f90 @@ -230,7 +230,7 @@ contains integer::start_send integer::chars_sent_this_time, chars_sending - integer::i, bytes + integer::i integer::string_length character, dimension(bufsize)::buffer -- cgit v1.2.3