anchor.diff:            ! Call objective function
             status_c = func_pointer(dim_c, point, func_value, &
                  callback_data, status_message_c, status_message_length_c)
  +          ! Terminate optimization on any error
             if (status_c /= LBFGSB_STATUS_SUCCESS) exit
   
             ! Call objective function gradient
             status_c = grad_pointer(dim_c, point, grad_value, &
                  callback_data, status_message_c, status_message_length_c)
  +          ! Terminate optimization on any error
             if (status_c /= LBFGSB_STATUS_SUCCESS) exit
          case ('WARNING')
             ! TODO handle warnings
          case ('NEW_X')
  -          ! TODO handle logging
  +          ! Call the logging function if one was given
  +          if (c_associated(log_function)) then
  +             ! Compute some of the values not otherwise available
  +             step_length = real_state(4) * real_state(14)
  +             f_delta = abs(real_state(2) - func_value) / &
  +                  max(abs(real_state(2)), abs(func_value), 1d0)
  +             ! Log the information
  +             status_c = log_pointer( &
  +                  int_state(30), int_state(36), int_state(34), step_length, &
  +                  dim_c, point, func_value, grad_value, &
  +                  f_delta, real_state(3), real_state(13), g_tolerance_c &
  +                  )
  +             ! Terminate optimization on any error
  +             if (status_c /= LBFGSB_STATUS_SUCCESS) exit
  +          end if
          end select
       end do
       ! End optimization
  
positive.code:   !
    !    |P(g(x[i]))|inf <= g_tolerance
    !
    !    where P(g(x)) is the projected gradient at x.
    !
    ! 'initial_point_c': Point from which minimization starts, x[0].
    !
  
negative.code:        integer(c_int) :: status
       end function objective_gradient_c
  
       ! Signature of logging C callback that (optionally) logs
       ! information about each iteration in the optimization.
       !
       ! 'iteration': Number of current iteration.
       !
       ! 'fg_evals': Number of evaluations in this iteration.  Each
       !    evaluation is both a function and a gradient call.
       !
       ! 'fg_evals_total': Total number of evaluations so far.
       !
       ! 'step': Length of the step taken towards the minimum in this
       !    iteration.
       !
       ! 'dim': Dimensionality of the optimization space; the size of the
       !    arrays used for points, gradients.
       !
       ! 'x': Current point in optimization space.
       !
       ! 'f': Current objective function value.
       !
       ! 'g': Current objective function gradient.
       !
       ! 'f_delta': Change in objective function value over this
       !    iteration.
       !
       ! 'f_delta_bound': Upper bound on 'f_delta' required for
       !    convergence.
       !
       ! 'g_norm': Infinity norm of the projected gradient.
       !
       ! 'g_norm_bound': Upper bound on 'g_norm' required for convergence.
       !
       ! 'error': Returns the error status, one of LBFGSB_STATUS_SUCCESS
       !    or LBFGSB_STATUS_INTERNAL_ERROR.  Returning an error will take
       !    down the whole optimization, so only return an error if the
       !    optimization cannot continue.  Logging issues may or may not
       !    be that serious depending on the application.
       function log_function_c(iteration, fg_evals, fg_evals_total, step, &
            dim, x, f, g, &
            f_delta, f_delta_bound, g_norm, g_norm_bound) &
            result(error) bind(c)
         use, intrinsic :: iso_c_binding
         implicit none
         integer(c_int), intent(in), value :: iteration, fg_evals, &
              fg_evals_total, dim
         real(c_double), intent(in), value :: step, f, &
              f_delta, f_delta_bound, g_norm, g_norm_bound
         real(c_double), intent(in) :: x(dim), g(dim)
         integer(c_int) :: error
       end function log_function_c
  
    end interface
  
    ! Private constants
  