Skip to content

Commit

Permalink
Merge pull request #26 from darbyjohnston/logging
Browse files Browse the repository at this point in the history
Add more logging
  • Loading branch information
darbyjohnston authored Nov 25, 2024
2 parents 408978a + eb07f03 commit e646ca8
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 12 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ jobs:
- name: Upload artifact URL
run: echo 'Artifact URL ${{ steps.linux-package-artifact.outputs.artifact-url }}'

macos-12-build:
runs-on: macos-12
macos-13-build:
runs-on: macos-13

steps:
- uses: actions/checkout@v4
Expand All @@ -103,8 +103,8 @@ jobs:
run: |
build-Debug/tests/toucan-test/toucan-test toucan/data
macos-14-build:
runs-on: macos-14
macos-build:
runs-on: macos-latest

steps:
- uses: actions/checkout@v4
Expand All @@ -130,8 +130,8 @@ jobs:
run: |
build-Debug/tests/toucan-test/toucan-test toucan/data
macos-14-package:
runs-on: macos-14
macos-package:
runs-on: macos-latest

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ toucan-render Transition.otio - -y4m 444 | ffmpeg -y -i pipe: output.mov
```
* `Transition.otio`: The input timeline file.
* `-`: Write to standard out instead of a file.
* '-y4m 444': Set the pixel format of the output images. Possible values: 422,
* `-y4m 444`: Set the pixel format of the output images. Possible values: 422,
444, 444alpha, 444p16
* `-y`: Overwrite the output file if it already exists.
* `-i pipe:`: Read from standard input instead of a file.
Expand Down
2 changes: 2 additions & 0 deletions lib/toucan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ set(HEADERS
ImageEffectHost.h
ImageGraph.h
ImageNode.h
LRUCache.h
LRUCacheInline.h
MemoryMap.h
MessageLog.h
Plugin.h
Expand Down
8 changes: 6 additions & 2 deletions lib/toucan/ImageEffectHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,13 @@ namespace toucan
}
findPlugins(path, pluginPaths);
}
if (pluginPaths.empty() && _options.log)
{
_options.log->log(logPrefix, " No plugins found");
}

// Load the plugins.
if (_options.log)
if (!pluginPaths.empty() && _options.log)
{
_options.log->log(logPrefix, "Loading plugins...");
}
Expand Down Expand Up @@ -171,7 +175,7 @@ namespace toucan
}

// Initialize the plugins.
if (_options.log)
if (!_plugins.empty() && _options.log)
{
_options.log->log(logPrefix, "Initializing plugins...");
}
Expand Down
5 changes: 2 additions & 3 deletions lib/toucan/ImageGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
#pragma once

#include <toucan/ImageNode.h>
#include <toucan/LRUCache.h>
#include <toucan/MessageLog.h>
#include <toucan/TimelineWrapper.h>

#include <dtk/core/LRUCache.h>

#include <opentimelineio/mediaReference.h>
#include <opentimelineio/track.h>
#include <opentimelineio/transition.h>
Expand Down Expand Up @@ -67,6 +66,6 @@ namespace toucan
OTIO_NS::TimeRange _timeRange;
ImageGraphOptions _options;
IMATH_NAMESPACE::V2i _imageSize = IMATH_NAMESPACE::V2i(0, 0);
dtk::LRUCache<OTIO_NS::MediaReference*, std::shared_ptr<ReadNode> > _loadCache;
LRUCache<OTIO_NS::MediaReference*, std::shared_ptr<ReadNode> > _loadCache;
};
}
54 changes: 54 additions & 0 deletions lib/toucan/LRUCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the toucan project.

#pragma once

#include <cstdint>
#include <map>
#include <vector>

namespace toucan
{
//! Least recently used (LRU) cache.
template<typename T, typename U>
class LRUCache
{
public:
//! \name Size
///@{

size_t getMax() const;
size_t getSize() const;
size_t getCount() const;
float getPercentage() const;

void setMax(size_t);

///@}

//! \name Contents
///@{

bool contains(const T& key) const;
bool get(const T& key, U& value) const;

void add(const T& key, const U& value, size_t size = 1);
void remove(const T& key);
void clear();

std::vector<T> getKeys() const;
std::vector<U> getValues() const;

///@}

private:
void _maxUpdate();

size_t _max = 10000;
std::map<T, std::pair<U, size_t> > _map;
mutable std::map<T, int64_t> _counts;
mutable int64_t _counter = 0;
};
}

#include <toucan/LRUCacheInline.h>
151 changes: 151 additions & 0 deletions lib/toucan/LRUCacheInline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the toucan project.

#include <algorithm>

namespace toucan
{
template<typename T, typename U>
inline std::size_t LRUCache<T, U>::getMax() const
{
return _max;
}

template<typename T, typename U>
inline std::size_t LRUCache<T, U>::getSize() const
{
size_t out = 0;
for (const auto& i : _map)
{
out += i.second.second;
}
return out;
}

template<typename T, typename U>
inline std::size_t LRUCache<T, U>::getCount() const
{
return _map.size();
}

template<typename T, typename U>
inline float LRUCache<T, U>::getPercentage() const
{
return getSize() / static_cast<float>(_max) * 100.F;
}

template<typename T, typename U>
inline void LRUCache<T, U>::setMax(std::size_t value)
{
if (value == _max)
return;
_max = value;
_maxUpdate();
}

template<typename T, typename U>
inline bool LRUCache<T, U>::contains(const T& key) const
{
return _map.find(key) != _map.end();
}

template<typename T, typename U>
inline bool LRUCache<T, U>::get(const T& key, U& value) const
{
auto i = _map.find(key);
if (i != _map.end())
{
value = i->second.first;
auto j = _counts.find(key);
if (j != _counts.end())
{
++_counter;
j->second = _counter;
}
return true;
}
return i != _map.end();
}

template<typename T, typename U>
inline void LRUCache<T, U>::add(const T& key, const U& value, size_t size)
{
_map[key] = std::make_pair(value, size);
++_counter;
_counts[key] = _counter;
_maxUpdate();
}

template<typename T, typename U>
inline void LRUCache<T, U>::remove(const T& key)
{
const auto i = _map.find(key);
if (i != _map.end())
{
_map.erase(i);
}
const auto j = _counts.find(key);
if (j != _counts.end())
{
_counts.erase(j);
}
_maxUpdate();
}

template<typename T, typename U>
inline void LRUCache<T, U>::clear()
{
_map.clear();
}

template<typename T, typename U>
inline std::vector<T> LRUCache<T, U>::getKeys() const
{
std::vector<T> out;
for (const auto& i : _map)
{
out.push_back(i.first);
}
return out;
}

template<typename T, typename U>
inline std::vector<U> LRUCache<T, U>::getValues() const
{
std::vector<U> out;
for (const auto& i : _map)
{
out.push_back(i.second.first);
}
return out;
}

template<typename T, typename U>
inline void LRUCache<T, U>::_maxUpdate()
{
size_t size = getSize();
if (size > _max)
{
std::map<int64_t, T> sorted;
for (const auto& i : _counts)
{
sorted[i.second] = i.first;
}
while (getSize() > _max)
{
auto begin = sorted.begin();
auto i = _map.find(begin->second);
if (i != _map.end())
{
_map.erase(i);
}
auto j = _counts.find(begin->second);
if (j != _counts.end())
{
_counts.erase(j);
}
sorted.erase(begin);
}
}
}
}

0 comments on commit e646ca8

Please sign in to comment.