/usr/share/ada/adainclude/aws/aws-log.adb is in libaws3.2.0-dev 3.2.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | ------------------------------------------------------------------------------
-- Ada Web Server --
-- --
-- Copyright (C) 2000-2013, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify --
-- it under terms of the GNU General Public License as published by the --
-- Free Software Foundation; either version 3, or (at your option) any --
-- later version. This library is distributed in the hope that it will be --
-- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- --
-- --
-- --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- --
-- --
-- --
-- --
-- --
-- --
------------------------------------------------------------------------------
pragma Ada_2012;
with Ada.Calendar;
with Ada.Command_Line;
with Ada.Directories;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Text_IO.C_Streams;
with GNAT.Calendar.Time_IO;
package body AWS.Log is
procedure Check_Split (Log : in out Object; Now : Ada.Calendar.Time);
-- Split log file if necessary
procedure Write_Callback (Log : in out Object; Data : in out Fields_Table);
-- Write extended format record to log file and prepare record for the next
-- data. It is not allowed to use same Fields_Table with different extended
-- logs.
procedure Write_File (Log : in out Object; Data : in out Fields_Table);
-- Write extended format record to log file and prepare record for the next
-- data. It is not allowed to use same Fields_Table with different extended
-- logs.
procedure Write_Log
(Log : in out Object;
Now : Calendar.Time;
Data : String);
-- Write data into the log file, change log file depending on the log file
-- split mode and Now.
function Get_Position (File : Ada.Text_IO.File_Type) return Natural
with Inline;
-- Returns current write position in the text file
-----------------
-- Check_Split --
-----------------
procedure Check_Split (Log : in out Object; Now : Ada.Calendar.Time) is
Keep_Split : Split_Mode;
begin
if (Log.Split = Daily
and then Log.Current_Tag /= Calendar.Day (Now))
or else
(Log.Split = Monthly
and then Log.Current_Tag /= Calendar.Month (Now))
then
-- Could not call Stop, because Stop would write to log again and
-- it cause unlimited recursion.
Text_IO.Close (Log.File);
Start (Log => Log,
Split => Log.Split,
Size_Limit => Log.Size_Limit,
File_Directory => To_String (Log.File_Directory),
Filename_Prefix => To_String (Log.Filename_Prefix),
Auto_Flush => Log.Auto_Flush);
elsif Log.Size_Limit > 0
and then Get_Position (Log.File) > Log.Size_Limit
then
Keep_Split := Log.Split;
Text_IO.Close (Log.File);
Start (Log => Log,
Split => Each_Run,
Size_Limit => Log.Size_Limit,
File_Directory => To_String (Log.File_Directory),
Filename_Prefix => To_String (Log.Filename_Prefix),
Auto_Flush => Log.Auto_Flush);
Log.Split := Keep_Split;
end if;
end Check_Split;
--------------
-- Filename --
--------------
function Filename (Log : Object) return String is
begin
if Text_IO.Is_Open (Log.File) then
return Text_IO.Name (Log.File);
elsif Log.Writer /= null then
return To_String (Log.Writer_Name);
else
return "";
end if;
end Filename;
--------------
-- Finalize --
--------------
overriding procedure Finalize (Log : in out Object) is
begin
Stop (Log);
end Finalize;
-----------
-- Flush --
-----------
procedure Flush (Log : in out Object) is
use Text_IO;
begin
if Log.Writer /= null or else Log.Auto_Flush then
return;
end if;
Log.Semaphore.Seize;
if Is_Open (Log.File) then
Flush (Log.File);
end if;
Log.Semaphore.Release;
exception
when others =>
Log.Semaphore.Release;
raise;
end Flush;
------------------
-- Get_Position --
------------------
function Get_Position (File : Ada.Text_IO.File_Type) return Natural is
use Ada.Text_IO.C_Streams;
begin
return Natural (ICS.ftell (C_Stream (File)));
end Get_Position;
---------------
-- Is_Active --
---------------
function Is_Active (Log : Object) return Boolean is
begin
return Text_IO.Is_Open (Log.File) or else Log.Writer /= null;
end Is_Active;
----------
-- Mode --
----------
function Mode (Log : Object) return Split_Mode is
begin
return Log.Split;
end Mode;
--------------------
-- Register_Field --
--------------------
procedure Register_Field (Log : in out Object; Id : String) is
Position : Strings_Positive.Cursor;
Success : Boolean;
begin
Log.Extended_Fields.Insert
(Id, Natural (Log.Extended_Fields.Length) + 1, Position, Success);
end Register_Field;
---------------
-- Set_Field --
---------------
procedure Set_Field
(Log : Object; Data : in out Fields_Table; Id, Value : String)
is
Ext_Len : constant Natural := Natural (Log.Extended_Fields.Length);
Data_Len : constant Natural := Natural (Data.Values.Length);
begin
if Ext_Len = 0 then
return;
end if;
if Data_Len = 0 then
-- Fields record is not initialized. We have to define fields set
-- with empty "-" values.
for J in 1 .. Ext_Len loop
Data.Values.Append ("-");
end loop;
elsif Data_Len /= Ext_Len then
-- Looks like the record was used with different log file
raise Constraint_Error;
end if;
declare
CSN : constant Strings_Positive.Cursor :=
Log.Extended_Fields.Find (Id);
begin
if Value /= "" and then Strings_Positive.Has_Element (CSN) then
Data.Values.Replace_Element
(Strings_Positive.Element (CSN), Value);
end if;
end;
end Set_Field;
-----------------------
-- Set_Header_Fields --
-----------------------
procedure Set_Header_Fields
(Log : Object;
Data : in out Fields_Table;
Prefix : String;
Header : AWS.Headers.List)
is
procedure Process (Name, Value : String);
-------------
-- Process --
-------------
procedure Process (Name, Value : String) is
begin
Set_Field
(Log, Data, Prefix & '(' & Name & ')',
AWS.Utils.Quote (Value, """"""));
end Process;
begin
Header.Iterate_Names (", ", Process'Access);
end Set_Header_Fields;
-----------
-- Start --
-----------
procedure Start
(Log : in out Object;
Split : Split_Mode := None;
Size_Limit : Natural := 0;
File_Directory : String := Not_Specified;
Filename_Prefix : String := Not_Specified;
Auto_Flush : Boolean := False)
is
function Get_Prefix return String;
-- Returns the prefix to use for the log filename
function Log_Prefix (Prefix : String) return String;
-- Returns the prefix to be added before the log filename. The returned
-- value is the executable name without directory and filetype if Prefix
-- is Not_Specified otherwise Prefix is returned.
Now : constant Calendar.Time := Calendar.Clock;
----------------
-- Get_Prefix --
----------------
function Get_Prefix return String is
begin
if Split = None then
return Utils.Normalized_Directory (File_Directory)
& Log_Prefix (Filename_Prefix);
else
return Utils.Normalized_Directory (File_Directory)
& Log_Prefix (Filename_Prefix) & '-'
& GNAT.Calendar.Time_IO.Image (Now, "%Y-%m-%d");
end if;
end Get_Prefix;
----------------
-- Log_Prefix --
----------------
function Log_Prefix (Prefix : String) return String is
function Prog_Name return String;
-- Return current program name
---------------
-- Prog_Name --
---------------
function Prog_Name return String is
Name : constant String := Ada.Command_Line.Command_Name;
First : Natural;
Last : Natural;
begin
First := Strings.Fixed.Index
(Name, Strings.Maps.To_Set ("/\"), Going => Strings.Backward);
if First = 0 then
First := Name'First;
else
First := First + 1;
end if;
Last := Strings.Fixed.Index
(Name (First .. Name'Last), ".", Strings.Backward);
if Last = 0 then
Last := Name'Last;
else
Last := Last - 1;
end if;
return Name (First .. Last);
end Prog_Name;
begin
if Prefix = Not_Specified then
return "";
else
declare
K : constant Natural := Strings.Fixed.Index (Prefix, "@");
begin
if K = 0 then
return Prefix;
else
return Prefix (Prefix'First .. K - 1)
& Prog_Name & Prefix (K + 1 .. Prefix'Last);
end if;
end;
end if;
end Log_Prefix;
Prefix : constant String := Get_Prefix;
Filename : Unbounded_String;
Time_Part : String (1 .. 7);
begin
Log.Filename_Prefix := To_Unbounded_String (Filename_Prefix);
Log.File_Directory := To_Unbounded_String (File_Directory);
Log.Split := Split;
Log.Size_Limit := Size_Limit;
Log.Auto_Flush := Auto_Flush;
Log.Header_Written := False;
Log.Stop_Has_Been_Called := False;
Filename := To_Unbounded_String (Prefix & ".log");
case Split is
when None =>
null;
when Each_Run =>
if Directories.Exists (To_String (Filename)) then
Time_Part := GNAT.Calendar.Time_IO.Image (Now, "-%H%M%S");
Filename := To_Unbounded_String (Prefix & Time_Part & ".log");
for K in 1 .. 99 loop
exit when not Directories.Exists (To_String (Filename));
Filename :=
To_Unbounded_String
(Prefix & Time_Part & '-' & Utils.Image (K) & ".log");
end loop;
end if;
when Daily =>
Log.Current_Tag := Ada.Calendar.Day (Now);
when Monthly =>
Log.Current_Tag := Ada.Calendar.Month (Now);
end case;
Text_IO.Open (Log.File, Text_IO.Append_File, To_String (Filename));
exception
when Text_IO.Name_Error =>
Text_IO.Create (Log.File, Text_IO.Out_File, To_String (Filename));
end Start;
-----------
-- Start --
-----------
procedure Start
(Log : in out Object;
Writer : Callback;
Name : String) is
begin
Log.Writer := Writer;
Log.Writer_Name := To_Unbounded_String (Name);
Log.Header_Written := False;
Log.Stop_Has_Been_Called := False;
end Start;
----------
-- Stop --
----------
procedure Stop (Log : in out Object) is
begin
if not Log.Stop_Has_Been_Called then
if Log.Writer = null then
if Text_IO.Is_Open (Log.File) then
Write (Log, "Stop logging.");
Text_IO.Close (Log.File);
end if;
else
Log.Writer ("Stop logging.");
Log.Writer := null;
end if;
end if;
Log.Stop_Has_Been_Called := True;
end Stop;
-----------
-- Write --
-----------
-- Here is the log format compatible with Apache:
--
-- 127.0.0.1 - - [25/Apr/1998:15:37:29 +0200] "GET / HTTP/1.0" 200 1363
--
-- See http://httpd.apache.org/docs/trunk/logs.html
procedure Write
(Log : in out Object;
Connect_Stat : Status.Data;
Answer : Response.Data) is
begin
Write (Log, Connect_Stat,
Response.Status_Code (Answer),
Response.Content_Length (Answer));
end Write;
procedure Write
(Log : in out Object;
Connect_Stat : Status.Data;
Status_Code : Messages.Status_Code;
Content_Length : Response.Content_Length_Type)
is
use type Response.Content_Length_Type;
function Length_Image return String is
(if Content_Length = Response.Undefined_Length
then ""
else Utils.Image (Content_Length)) with Inline;
begin
Write
(Log, Connect_Stat, Messages.Image (Status_Code) & ' ' & Length_Image);
end Write;
procedure Write
(Log : in out Object;
Connect_Stat : Status.Data;
Data : String)
is
Now : constant Calendar.Time := Calendar.Clock;
function Authorization_Name return String with Inline;
------------------------
-- Authorization_Name --
------------------------
function Authorization_Name return String is
Result : constant String := Status.Authorization_Name (Connect_Stat);
begin
if Result = "" then
return "-";
else
return Result;
end if;
end Authorization_Name;
Log_Message : constant String :=
AWS.Status.Peername (Connect_Stat)
& " - " & Authorization_Name
& " ["
& GNAT.Calendar.Time_IO.Image (Now, "%d/%b/%Y:%T")
& "] """
& Status.Method (Connect_Stat)
& ' '
& Status.URI (Connect_Stat) & " "
& Status.HTTP_Version (Connect_Stat) & """ "
& Data;
begin
if Log.Writer = null then
Write_Log (Log, Now, Log_Message);
else
Log.Writer (Log_Message);
end if;
end Write;
procedure Write (Log : in out Object; Data : String) is
Now : constant Calendar.Time := Calendar.Clock;
Log_Message : constant String := "["
& GNAT.Calendar.Time_IO.Image
(Now, "%d/%b/%Y:%T") & "] " & Data;
begin
if Log.Writer = null then
Write_Log (Log, Now, Log_Message);
else
Log.Writer (Log_Message);
end if;
end Write;
-- Here is the extended log format:
--
-- #Version: 1.0
-- #Date: 12-Jan-1996 00:00:00
-- #Fields: time cs-method cs-uri
-- 00:34:23 GET /foo/bar.html
procedure Write (Log : in out Object; Data : in out Fields_Table) is
begin
if Log.Writer = null then
Write_File (Log, Data);
else
Write_Callback (Log, Data);
end if;
end Write;
----------------------
-- Write_Callback --
----------------------
procedure Write_Callback
(Log : in out Object; Data : in out Fields_Table)
is
use GNAT.Calendar.Time_IO;
Length : constant Natural := Natural (Log.Extended_Fields.Length);
Message : Unbounded_String := Null_Unbounded_String;
Now : Ada.Calendar.Time;
First_Field : Boolean := True;
procedure Write_And_Clear (Position : SV.Cursor);
---------------------
-- Write_And_Clear --
---------------------
procedure Write_And_Clear (Position : SV.Cursor) is
begin
if First_Field then
First_Field := False;
Append (Message, SV.Element (Position));
else
Append (Message, ' ' & SV.Element (Position));
end if;
Data.Values.Replace_Element (Position, "-");
end Write_And_Clear;
begin
if Length = 0 then
-- It is not extended log
return;
end if;
Now := Ada.Calendar.Clock;
if not Log.Header_Written then
Log.Header_Written := True;
Log.Writer ("#Software: AWS (Ada Web Server) v" & Version);
Log.Writer ("#Date: " & Image (Now, ISO_Date & " %T"));
Log.Writer ("#Fields:");
declare
Fields : Unbounded_String := Null_Unbounded_String;
Order : array (1 .. Length) of Strings_Positive.Cursor;
procedure Process (Position : Strings_Positive.Cursor);
-------------
-- Process --
-------------
procedure Process (Position : Strings_Positive.Cursor)
is
begin
Order (Strings_Positive.Element (Position)) := Position;
end Process;
begin
Log.Extended_Fields.Iterate (Process'Access);
for J in Order'Range loop
Append (Fields, ' ' & Strings_Positive.Key (Order (J)));
end loop;
Log.Writer (To_String (Fields));
end;
end if;
-- Set date and time fields if the used does not fill it
declare
CSN : Strings_Positive.Cursor := Log.Extended_Fields.Find ("date");
P : Positive;
begin
if Strings_Positive.Has_Element (CSN) then
P := Strings_Positive.Element (CSN);
if Data.Values.Element (P) = "-" then
Data.Values.Replace_Element (P, Image (Now, ISO_Date));
end if;
end if;
CSN := Log.Extended_Fields.Find ("time");
if Strings_Positive.Has_Element (CSN) then
P := Strings_Positive.Element (CSN);
if Data.Values.Element (P) = "-" then
Data.Values.Replace_Element (P, Image (Now, "%T"));
end if;
end if;
end;
Data.Values.Iterate (Write_And_Clear'Access);
Log.Writer (To_String (Message));
end Write_Callback;
------------------
-- Write_File --
------------------
procedure Write_File (Log : in out Object; Data : in out Fields_Table) is
use GNAT.Calendar.Time_IO;
Length : constant Natural := Natural (Log.Extended_Fields.Length);
Now : Ada.Calendar.Time;
First_Field : Boolean := True;
procedure Write_And_Clear (Position : SV.Cursor);
---------------------
-- Write_And_Clear --
---------------------
procedure Write_And_Clear (Position : SV.Cursor) is
begin
if First_Field then
First_Field := False;
Text_IO.Put (Log.File, SV.Element (Position));
else
Text_IO.Put (Log.File, ' ' & SV.Element (Position));
end if;
Data.Values.Replace_Element (Position, "-");
end Write_And_Clear;
begin
if Length = 0 then
-- It is not extended log
return;
end if;
Log.Semaphore.Seize;
if Text_IO.Is_Open (Log.File) then
Now := Ada.Calendar.Clock;
Check_Split (Log, Now);
if not Log.Header_Written then
Log.Header_Written := True;
Text_IO.Put_Line (Log.File, "#Version: 1.0");
Text_IO.Put_Line
(Log.File, "#Software: AWS (Ada Web Server) v" & Version);
Text_IO.Put_Line
(Log.File, "#Date: " & Image (Now, ISO_Date & " %T"));
Text_IO.Put (Log.File, "#Fields:");
declare
Order : array (1 .. Length) of Strings_Positive.Cursor;
procedure Process (Position : Strings_Positive.Cursor);
-------------
-- Process --
-------------
procedure Process (Position : Strings_Positive.Cursor) is
begin
Order (Strings_Positive.Element (Position)) := Position;
end Process;
begin
Log.Extended_Fields.Iterate (Process'Access);
for J in Order'Range loop
Text_IO.Put
(Log.File, ' ' & Strings_Positive.Key (Order (J)));
end loop;
Text_IO.New_Line (Log.File);
end;
end if;
-- Set date and time fields if the used does not fill it
declare
CSN : Strings_Positive.Cursor := Log.Extended_Fields.Find ("date");
P : Positive;
begin
if Strings_Positive.Has_Element (CSN) then
P := Strings_Positive.Element (CSN);
if Data.Values.Element (P) = "-" then
Data.Values.Replace_Element (P, Image (Now, ISO_Date));
end if;
end if;
CSN := Log.Extended_Fields.Find ("time");
if Strings_Positive.Has_Element (CSN) then
P := Strings_Positive.Element (CSN);
if Data.Values.Element (P) = "-" then
Data.Values.Replace_Element (P, Image (Now, "%T"));
end if;
end if;
end;
Data.Values.Iterate (Write_And_Clear'Access);
Text_IO.New_Line (Log.File);
if Log.Auto_Flush then
Text_IO.Flush (Log.File);
end if;
end if;
Log.Semaphore.Release;
exception
when others =>
Log.Semaphore.Release;
raise;
end Write_File;
---------------
-- Write_Log --
---------------
procedure Write_Log
(Log : in out Object;
Now : Calendar.Time;
Data : String) is
begin
Log.Semaphore.Seize;
if Text_IO.Is_Open (Log.File) then
Check_Split (Log, Now);
Text_IO.Put_Line (Log.File, Data);
if Log.Auto_Flush then
Text_IO.Flush (Log.File);
end if;
end if;
Log.Semaphore.Release;
exception
when others =>
Log.Semaphore.Release;
raise;
end Write_Log;
end AWS.Log;
|