Skip to content

Commit d20faf8

Browse files
authored
Merge pull request #243 from jvdp1/logger2
Logger: support of new_line() in the output of the logger
2 parents d0cc617 + f7deccc commit d20faf8

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

src/stdlib_logger.f90

+37-22
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ subroutine format_output_string( self, unit, string, procedure_name, &
523523
character(*), intent(in) :: procedure_name
524524
character(*), intent(in) :: col_indent
525525

526-
integer :: count, indent_len, index, iostat, length, remain
526+
integer :: count, indent_len, index_, iostat, length, remain
527527
character(256) :: iomsg
528528

529529
length = len_trim(string)
@@ -544,27 +544,32 @@ subroutine format_output_string( self, unit, string, procedure_name, &
544544

545545
subroutine format_first_line()
546546

547-
if ( length <= self % max_width .or. self % max_width == 0 ) then
547+
if ( self % max_width == 0 .or. &
548+
( length <= self % max_width .and. &
549+
index( string(1:length), new_line('a')) == 0 ) ) then
548550
write( unit, '(a)', err=999, iostat=iostat, iomsg=iomsg ) &
549551
string(1:length)
550552
remain = 0
551553
return
552554
else
553555

554-
do index=self % max_width, 1, -1
555-
if ( string(index:index) == ' ' ) exit
556-
end do
556+
index_ = index( string(1:min(length, self % max_width)), new_line('a'))
557+
if ( index_ == 0 ) then
558+
do index_=self % max_width, 1, -1
559+
if ( string(index_:index_) == ' ' ) exit
560+
end do
561+
end if
557562

558-
if ( index == 0 ) then
563+
if ( index_ == 0 ) then
559564
write( unit, '(a)', err=999, iostat=iostat, iomsg=iomsg ) &
560565
string(1:self % max_width)
561566
count = self % max_width
562567
remain = length - count
563568
return
564569
else
565570
write( unit, '(a)', err=999, iostat=iostat, iomsg=iomsg ) &
566-
string(1:index-1)
567-
count = index
571+
string(1:index_-1)
572+
count = index_
568573
remain = length - count
569574
return
570575
end if
@@ -585,20 +590,24 @@ subroutine format_subsequent_line()
585590
return
586591
else
587592

588-
do index=count+self % max_width, count+1, -1
589-
if ( string(index:index) == ' ' ) exit
590-
end do
593+
index_ = count + index( string(count+1:count+self % max_width), &
594+
new_line('a'))
595+
if(index_ == count) then
596+
do index_=count+self % max_width, count+1, -1
597+
if ( string(index_:index_) == ' ' ) exit
598+
end do
599+
end if
591600

592-
if ( index == count ) then
601+
if ( index_ == count ) then
593602
write( unit, '(a)', err=999, iostat=iostat, iomsg=iomsg ) &
594603
string(count+1:count+self % max_width)
595604
count = count + self % max_width
596605
remain = length - count
597606
return
598607
else
599608
write( unit, '(a)', err=999, iostat=iostat, iomsg=iomsg ) &
600-
string(count+1:index-1)
601-
count = index
609+
string(count+1:index_-1)
610+
count = index_
602611
remain = length - count
603612
return
604613
end if
@@ -611,29 +620,35 @@ end subroutine format_subsequent_line
611620

612621
subroutine indent_format_subsequent_line()
613622

614-
if ( remain <= self % max_width - indent_len ) then
623+
if ( index( string(count+1:length), new_line('a')) == 0 .and. &
624+
remain <= self % max_width - indent_len ) then
615625
write( unit, '(a)', err=999, iostat=iostat, iomsg=iomsg ) &
616626
col_indent // string(count+1:length)
617627
count = length
618628
remain = 0
619629
return
620630
else
621631

622-
do index=count+self % max_width-indent_len, count+1, -1
623-
if ( string(index:index) == ' ' ) exit
624-
end do
632+
index_ = count + index( string(count+1: &
633+
min ( length, count+self % max_width - indent_len) ), &
634+
new_line('a'))
635+
if(index_ == count) then
636+
do index_=count+self % max_width-indent_len, count+1, -1
637+
if ( string(index_:index_) == ' ' ) exit
638+
end do
639+
end if
625640

626-
if ( index == count ) then
641+
if ( index_ == count ) then
627642
write( unit, '(a)', err=999, iostat=iostat, iomsg=iomsg ) &
628-
col_indent // &
643+
col_indent // &
629644
string(count+1:count+self % max_width-indent_len)
630645
count = count + self % max_width - indent_len
631646
remain = length - count
632647
return
633648
else
634649
write( unit, '(a)', err=999, iostat=iostat, iomsg=iomsg ) &
635-
col_indent // string(count+1:index-1)
636-
count = index
650+
col_indent // string(count+1:index_-1)
651+
count = index_
637652
remain = length - count
638653
return
639654
end if

src/tests/logger/test_stdlib_logger.f90

+24-1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ subroutine test_logging_configuration()
134134
module = 'N/A', &
135135
procedure = 'TEST_STDLIB_LOGGER' )
136136

137+
call global % log_information( 'This message should be output ' // &
138+
'to OUTPUT_UNIT, unlimited in width, not preceded by ' // &
139+
'a blank line, then by a time stamp, then by MODULE % ' // &
140+
'PROCEDURE, be prefixed by INFO. ' // new_line('a') // &
141+
'This is a new line of the same log message.', &
142+
module = 'N/A', &
143+
procedure = 'TEST_STDLIB_LOGGER' )
144+
137145
call global % configure( add_blank_line=.true., indent=.false., &
138146
max_width=72, time_stamp=.false. )
139147

@@ -142,7 +150,7 @@ subroutine test_logging_configuration()
142150
log_units=log_units )
143151

144152
if ( add_blank_line ) then
145-
write(*,*) 'ADD_BLANK_LINE is now .FALSE. as expected.'
153+
write(*,*) 'ADD_BLANK_LINE is now .TRUE. as expected.'
146154

147155
else
148156
error stop 'ADD_BLANKLINE is now .FALSE. contrary to expectations.'
@@ -191,6 +199,13 @@ subroutine test_logging_configuration()
191199
module = 'N/A', &
192200
procedure = 'TEST_STDLIB_LOGGER' )
193201

202+
call global % log_message( 'The last word of the first line ' // &
203+
new_line('a')//'should be "line". "Line"' // new_line('a') // &
204+
'is also the last word for the second line. The following ' // &
205+
'lines should be limited to 72 columns width.' , &
206+
module = 'N/A', &
207+
procedure = 'TEST_STDLIB_LOGGER' )
208+
194209
call global % configure( add_blank_line=.false., indent=.true., &
195210
max_width=72, time_stamp=.true. )
196211

@@ -202,6 +217,14 @@ subroutine test_logging_configuration()
202217
module = 'N/A', &
203218
procedure = 'TEST_STDLIB_LOGGER' )
204219

220+
call global % log_message( 'The last word of the first line ' // &
221+
new_line('a')//'should be "the". "Line"' // new_line('a') // &
222+
'should be the last word for the second line. The following ' // &
223+
'lines should be limited to 72 columns width. From the second ' //&
224+
'line, all lines should be indented by 4 columns.' ,&
225+
module = 'N/A', &
226+
procedure = 'TEST_STDLIB_LOGGER' )
227+
205228
end subroutine test_logging_configuration
206229

207230

0 commit comments

Comments
 (0)