ipopt_uout_journal.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_IPOPT_UOUT_JOURNAL_HPP
27 #define CASADI_IPOPT_UOUT_JOURNAL_HPP
28 
29 #include "casadi/core/casadi_logger.hpp"
30 #include <IpJournalist.hpp>
31 #include <cstdarg>
32 #include <cstdio>
33 #include <string>
34 #include <vector>
35 
37 namespace casadi {
38 
49  class UoutJournal : public Ipopt::Journal {
50  public:
51  UoutJournal(const std::string& name, Ipopt::EJournalLevel default_level)
52  : Ipopt::Journal(name, default_level) {}
53 
54  protected:
55  void PrintImpl(Ipopt::EJournalCategory, Ipopt::EJournalLevel, const char* str) override {
56  uout() << str;
57  }
58 
59  void PrintfImpl(Ipopt::EJournalCategory, Ipopt::EJournalLevel,
60  const char* pformat, va_list ap) override {
61  va_list ap2;
62  va_copy(ap2, ap);
63  int n = std::vsnprintf(nullptr, 0, pformat, ap2);
64  va_end(ap2);
65  if (n < 0) return;
66  std::vector<char> buf(n + 1);
67  std::vsnprintf(buf.data(), buf.size(), pformat, ap);
68  uout() << buf.data();
69  }
70 
71  void FlushBufferImpl() override {
72  uout().flush();
73  }
74  };
75 
76 } // namespace casadi
78 
79 #endif // CASADI_IPOPT_UOUT_JOURNAL_HPP
Ipopt journal printing through casadi::uout()
void PrintfImpl(Ipopt::EJournalCategory, Ipopt::EJournalLevel, const char *pformat, va_list ap) override
void FlushBufferImpl() override
UoutJournal(const std::string &name, Ipopt::EJournalLevel default_level)
void PrintImpl(Ipopt::EJournalCategory, Ipopt::EJournalLevel, const char *str) override
The casadi namespace.
Definition: archiver.cpp:28
std::string str(const T &v)
String representation, any type.
std::ostream & uout()